![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| awk: need to extract a line before a pattern | npn35 | Shell Programming and Scripting | 17 | 06-29-2008 11:38 PM |
| Extract Pattern Sequence | jaganadh | Shell Programming and Scripting | 5 | 12-10-2007 11:06 AM |
| Extract the Pattern | aajan | UNIX for Dummies Questions & Answers | 6 | 08-08-2007 05:47 AM |
| How to select lines in unix matches a pattern at a particular position | cs_banda | UNIX for Dummies Questions & Answers | 2 | 10-06-2006 04:28 PM |
| Please help! Sed extract a pattern | zhen | Shell Programming and Scripting | 11 | 09-18-2006 01:36 PM |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
Extract if pattern matches
Hi All,
I have an input below. I tried to use the awk below but it seems that it ;s not working. Can anybody help ? My concept here is to find the 2nd field of the last occurrence of such pattern " ** XXX ccc ccc cc cc ccc 2007 " . In this case, the 2nd field is " XXX ". With this "XXX" term stored as a variable, i want to print out the all lines with 2nd field having " XXX " term and its subsequent lines containing terms matching with " k= ". Expected output are highlighted as bold red in the input. Input: wwwwww 0999 k= 1 wwwwww ** XXX ccc ccc cc cc ccc 2007 wwwwww wwwwww 0001 k= 1 wwwwww 0002 k= 1 ** abc ccc cc cc cc cc 2007 wwwwww 0001 k= 1 wwwwww 0002 k= 1 wwwwww wwwwww 0003 k= 1 wwwwww ** XXX ccc ccc cc cc ccc 2007 wwwwww 0003 k= 1 wwwwww 0004 k= 1 0005 k= 1 Output: ** XXX ccc ccc cc cc ccc 2007 0001 k= 1 0002 k= 1 ** XXX ccc ccc cc cc ccc 2007 0003 k= 1 0004 k= 1 0005 k= 1 My AWK code: Code:
$NF == "2007" && $1 == "**" && NF == "8" {Field2 = $2}
$1 == "**" && $8 == "2007" && $2 == Field2 {
print ;
flag = 1;
next;
}
flag == 1 && $2 ~ /k=/ {print}
$1 == "**" && $8 == "2007" && $2 != Field2 {flag = 0}
|
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|