AWK script to print a single next word following a pattern match from Description field

Advertisements

I have below CSV file:

CreateDate, First Name, Last Name, Description
2023/03/25, Makvala, Anima, Your orders are 6157 and total value is 854257
2023/03/26, Leobwin, Saturnus, Your orders are 72 and total value is 1575675
2023/03/25, Boadicea, Pradip, Your orders are 52116 and total value is 29344357

I want below output

Orders: 6157, Total Value: 854257
Orders: 72, Total Value: 1575675
Orders: 52116, Total Value: 29344357

I already looked for a couple of similar questions on StackOverflow, but they are extracting the word considering there is only 1 field and then separating using orders are , but I have multiple fields and I want to get the values from Description field.

Any help would be much appreciated.

>Solution :

Like this with nawk, mawk, gawk, busybox awk:

awk -F, '
    NR>1{
        split($4, a, / +/)
        print "Orders:", a[5] ",", "Total Value:", a[10]
    }
' file

Output

Orders: 6157, Total value: 854257
Orders: 72, Total value: 1575675
Orders: 52116, Total value: 29344357

Leave a ReplyCancel reply