From: Harry on
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
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
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
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