ravi
ravi

Reputation: 6328

Working with AWK regex

I have a file in which have values in following format-

20/01/2012 01:14:27;UP;UserID;User=bob [email protected]

I want to pick each value from this file (not labels). By saying label, i mean to say that for string [email protected], i only want to pick [email protected] and for sting User=bob, i only want to pic bob. All the Space separated values are easy to pick but i am unable to pick the values separated by Semi colon. Below is the command i am using in awk-

awk '{print "1=",$1} /;/{print "2=",$2,"3=",$3}' sample_file

In $2, i am getting the complete string till bob and rest of the string is assigned to $3. Although i can work with substr provided with awk but i want to be on safe side, string length may vary. Can somebody tell me how to design such regex to parse my file.

Upvotes: 0

Views: 1201

Answers (1)

Steve
Steve

Reputation: 54392

You can set multiple delimiters using awk -F:

awk -F "[ \t;=]+" '{ print $1, $2, $3, $4, $5, $6, $7, $8 }' file.txt

Results:

value1 value2 value3 value4 label1 value5 label2 value6

EDIT:

You can remove anything before the equal signs using sub (/[^=]*=/,"", $i). This will allow you to just print the 'values':

awk 'BEGIN { FS="[ \t;]+"; OFS=" " } { for (i=1; i<=NF; i++) { sub (/[^=]*=/,"", $i); line = (line ? line OFS : "") $i } print line; line = "" }' file.txt

Results:

20/01/2012 01:14:27 UP UserID bob [email protected]

Upvotes: 2

Related Questions