Reputation: 469
This message1(name:"hello";age:10;US);
This message2(name:"hii";age:12;US;sdkfjd;l);
I wanted to extract word between age: and ; to get 10. The position of this word varies in each line. So I can't use $columnnumber. When I use this, it's not giving the required output.
awk -F'age:|;' '{print $2}'
Upvotes: 0
Views: 79
Reputation: 19194
As an alternative to awk, if your grep implementation supports perl regex:
grep -oP "(?<=age:)[0-9]+" File
10
or with GNU awk:
awk 'match($0, /age:"?([0-9]+)"?/, a) { print a[1] }' File
10
Upvotes: 0
Reputation: 5298
With awk:
awk -F".*age:|;|)" '{print $2}' File
Set the field seperator as 1).*age: (char sequence ending with age:
), 2);
and 3))
. Then just print the second field.
Example:
AMD$ cat File
This is a message(name:"hello";age:10;US);
This is a message(name:"hello";US;age:10);
This is a message(age:10;US;name:"hello");
AMD$ awk -F".*age:|;|)" '{print $2}' File
10
10
10
Upvotes: 1