Questionmark
Questionmark

Reputation: 884

How can I change the awk delimiter for a part of my script?

I have an input string that is formatted like this:

string1;string2"string3";string4

I want to parse this file to get the value of string3 using awk. To do this, I can first delimit by ;, print the second segment, and then delimit by " and print the second segment. Example using pipes:

$ echo 'string1;string2"string3";string4' | awk -F\; '{print $2}' | awk -F\" '{print $2}';
string3

I want to combine this into a single awk command, but I do not know how to change the field separator during my command. Is there syntax I can use in awk to change my separator?

Upvotes: 3

Views: 7077

Answers (2)

RavinderSingh13
RavinderSingh13

Reputation: 133710

Could you please try following and let me know how it goes then.

echo 'string1;string2"string3";string4' | awk -F'[;"]' '{print $3}'

So above is creating multiple delimiters by mentioning -F option in awk and then I am setting delimiters as chars(; ") so then string3 will be 3rd field and you could merge your awk like that. I hope this helps you.

EDIT: Apologies MODs/all, I am new to this site, so I am adding another alternative for this question's answer. Thank you Questionmark, it encourages me. So in case you have only have two occurrences of " in your string and you want to get rid of this delimiter then following could help you:

echo 'string1;string2"string3";string4' | awk '{match($0,/\".*\"/);print substr($0,RSTART+1,RLENGTH-2)}'

In the above code I am matching the regex using the match functionality of awk, so once it matches the specific string then I am printing the specific match(where RSTART and RLENGTH are the built-in variables in awk which will be set only when inside, the regex match is TRUE, so they are printed. I hope this will help too.

Upvotes: 2

anubhava
anubhava

Reputation: 785856

You can use split function inside awk:

s='string1;string2"string3";string4'
awk -F ';' 'split($2, a, /"/){print a[2]}' <<< "$s"

string3

As per the linked doc:

split(string, array [, fieldsep [, seps ] ])
Divide string into pieces separated by fieldsep and store the pieces in array and the separator strings in the seps array.

Upvotes: 7

Related Questions