0

Regarding to below command, I tried to insert space-separated contents of a log file into an array (arr), then make a condition to find and extract a specific string out of it. The problem is that the subject string is surrounded by single quotations and I don't know what is the proper syntax to involve those quotations in condition statement:

cat file.log | nawk '{split($0,arr," "); for (i=1;i<=NF;i++) if (arr[i]=='serviceid') printf ("%s,%s\n",arr[i],arr[i+1])}'

I've tried these:

 cat file.log | nawk '{split($0,arr," "); for (i=1;i<=NF;i++) if (arr[i]=="'serviceid'") printf ("%s,%s\n",arr[i],arr[i+1])}'

cat file.log | nawk '{split($0,arr," "); for (i=1;i<=NF;i++) if (arr[i]=="\'serviceid\'") printf ("%s,%s\n",arr[i],arr[i+1])}'

I also tried to split based on single quotation like split($0,arr,/\'/), but it also fails.

Log

This is a part of the log: 'serviceid': 1002, is the part I'm trying to navigate to. When I execute print arr[301] it returns 'serviceid':. But when I command for (i=1;i<=NF;i++) if (arr[i] == 'serviceid',) {print $i} I get syntax error around single quotes.

'originTransactionID': '00720170311', 'serviceFeeExpiryDate': datetime.datetime(2019, 12, 23, 12, 0, tzinfo=), 'accountFlagsBefore': {'activationStatusFlag': True, 'supervisionPeriodExpiryFlag': False, 'supervisionPeriodWarningActiveFlag': False, 'negativeBarringStatusFlag': False, 'serviceFeePeriodExpiryFlag': False, 'serviceFeePeriodWarningActiveFlag': False}, 'serviceid': 1002, 'supervisionExpiryDate': datetime.datetime(2019, 12, 23, 12, 0, tzinfo=), 'dedicatedAccountInformation': [{'startDate': datetime.datetime(9999, 12, 31, 0, 0, tzinfo=), 'dedicatedAccountValue1': '0', 'dedicatedAccountID': 1
3
  • 1
    It would be nice if you were to include a short example of the log file so we can test our code. Commented Mar 11, 2017 at 2:43
  • I put the log below. Thanks, Commented Mar 11, 2017 at 15:09
  • Use \047 where you want the quote. Usually assign that to a var in BEGIN and then use that :) Commented Mar 11, 2017 at 16:22

1 Answer 1

2

Just use a variable that contain the value you want:

awk -v str="'serviceid'" … …

That will avoid the need for complex quoting inside the script.
The whole awk script is inside single quotes, that makes the use of single quotes difficult.

Also, the example of log file that you posted, has a field with an ending colon 'serviceid':. If the fields are split on white space.
You can append that to the variable defined above or use the ~ (match) operator instead of the == (equal) operator. That operator (~) will match any field that contains 'serviceid'.

Like this:

nawk -vstr="'serviceid'" '
    $0~str{
        split($0,arr," "); 
        for (i=1;i<=NF;i++) 
            if (arr[i]~str) 
                printf ("%s,%s\n",arr[i],arr[i+1])
    }' file.log

Note that there is no need for a cat, as awk (nawk in this case) is perfectly able to read files.
Also, I added a $0~str to only process lines that contain such text.

In one line:

nawk -vstr="'serviceid'" '$0~str{ split($0,arr," "); for (i=1;i<=NF;i++) if (arr[i]~str) printf ("%s,%s\n",arr[i],arr[i+1]) }' infile
Sign up to request clarification or add additional context in comments.

2 Comments

@aschkant If the answer helped to solve your problem it is usual to "accept" the answer. Please read What should I do when someone answers my question?
Done, dear @sorontar ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.