Reputation: 967
I have a text file with the pattern starting '-- Host' and ending ';', and this pattern occurs so many times in the file as follow:
-- Host (first) Sid (queen1)
-- prince princess#/king 1/1
;
-- Host (first) Sid (queen2)
-- prince princess#/king 2/2
;
-- Host (first) Sid (queen3)
-- dir princess#/king 3/3
;
My desired result is as follow:
queen1 1/1
queen2 2/2
queen3 3/3
With AWK, I think I can specify the block including this patten like below.However, I got error in putting the second pattern in pattern.
Can you help me accomplish this requirement? Thanks!
BEGIN {
}
/-- Host/,/;/ { /prince/ { print $3 } }
END
Upvotes: 2
Views: 67
Reputation: 113984
$ awk -F'[ ()]+' '/-- Host/,/;/ { if (/-- Host/) printf "%s ",$5; else if (/prince/) print $4;}' file
queen1 1/1
queen2 2/2
queen3 3/3
-F'[ ()]+'
This tells awk to use any combination of any number of the blanks and parens as the field separator.
/-- Host/,/;/
This selects just groups of lines that start with -- Host
and end with ;
. The commands which follow in braces are only executed for lines in these groups.
{ if (/-- Host/) printf "%s ",$5; else if (/prince/) print $4;}
If the line contains -- Host
, then we print its fifth field. Otherwise, if it contains prince
, we print its fourth field.
Upvotes: 1