From: Harry on 18 May 2010 14:01 I am trying to use awk to print some records which do not contain a pattern CLUSTER(' '). I expect the runme script would print only record #2 below, excluding record #1. But it didn't work (i.e. record #1 was printed when I want to exclude it). $ cat runme PATTERN=CLUSTER\(\'\ \'\) awk -v exclude_this="$PATTERN" 'BEGIN {RS="";FS="\n"} ($0 !~ exclude_this) { for(i=1;i<=NF;i++) print $i; print "" } ' $ cat input.txt ../LPAR1-QM01.txt QLOCAL('Q1_RSP') CLUSTER(' ') ../LPAR2-QM02.txt QLOCAL('Q2_RSP') CLUSTER('TK01') $ sh runme < input.txt ../LPAR1-QM01.txt QLOCAL('Q1_RSP') CLUSTER(' ') ../LPAR2-QM02.txt QLOCAL('Q2_RSP') CLUSTER('TK01') $ What did I do wrong ? Please help to correct my script. TIA
From: Radoulov, Dimitre on 18 May 2010 14:41 On 18/05/2010 20.01, Harry wrote: > I am trying to use awk to print some records which do not contain > a pattern CLUSTER(' '). > I expect the runme script would print only record #2 below, > excluding record #1. > But it didn't work (i.e. record #1 was printed when I want to > exclude it). > > $ cat runme > PATTERN=CLUSTER\(\'\ \'\) > awk -v exclude_this="$PATTERN" 'BEGIN {RS="";FS="\n"} > ($0 !~ exclude_this) { for(i=1;i<=NF;i++) print $i; print "" } ' > > $ cat input.txt > ./LPAR1-QM01.txt > QLOCAL('Q1_RSP') > CLUSTER(' ') > > ./LPAR2-QM02.txt > QLOCAL('Q2_RSP') > CLUSTER('TK01') [...] This seems to work: p="CLUSTER\\\(' '\\\)" awk '$0 !~ p' RS= ORS='\n\n' p="$p" infile Regards Dimitre
From: Harry on 18 May 2010 23:05 Radoulov, Dimitre wrote... > >On 18/05/2010 20.01, Harry wrote: >> I am trying to use awk to print some records which do not contain >> a pattern CLUSTER(' '). >> $ cat runme >> PATTERN=CLUSTER\(\'\ \'\) >> awk -v exclude_this="$PATTERN" 'BEGIN {RS="";FS="\n"} >> ($0 !~ exclude_this) { for(i=1;i<=NF;i++) print $i; print "" } ' >> >> $ cat input.txt >> ./LPAR1-QM01.txt >> QLOCAL('Q1_RSP') >> CLUSTER(' ') >> >> ./LPAR2-QM02.txt >> QLOCAL('Q2_RSP') >> CLUSTER('TK01') >This seems to work: >p="CLUSTER\\\(' '\\\)" > >awk '$0 !~ p' RS= ORS='\n\n' p="$p" infile Yes, it works well. Thanks
From: Glenn Jackman on 19 May 2010 16:15 At 2010-05-18 02:01PM, "Harry" wrote: > I am trying to use awk to print some records which do not contain > a pattern CLUSTER(' '). > I expect the runme script would print only record #2 below, > excluding record #1. > But it didn't work (i.e. record #1 was printed when I want to > exclude it). > > $ cat runme > PATTERN=CLUSTER\(\'\ \'\) > awk -v exclude_this="$PATTERN" 'BEGIN {RS="";FS="\n"} > ($0 !~ exclude_this) { for(i=1;i<=NF;i++) print $i; print "" } ' If you're looking for a fixed string and not a regular expression, use string functions instead of the regex match operator: awk -v exclude_this="$PATTERN" ' BEGIN {RS="";FS="\n"} index($0, exclude_this) == 0 { for(i=1;i<=NF;i++) print $i print "" } ' -- Glenn Jackman Write a wise saying and your name will live forever. -- Anonymous
|
Pages: 1 Prev: Matching strings in a line and printing them Next: CLICK CLICK |