Operating System - HP-UX
1833875 Members
2915 Online
110063 Solutions
New Discussion

Re: awk - script sequence problem?

 
u856100
Frequent Advisor

awk - script sequence problem?

Hello,

I have another little annoying niggle in awk.

I can get around the problem, but for future reference, I would like to know why. I have the following script :

run with
# my_awk_script

cat $1 | awk -F"|" '
$1 ~ /^249$/ {printf("%s|%s|%s|%s DAN HOWARD|\n",$1,$2,$3,$4);next}
/^DAN HOWARD|$/ {next}
/^Dan Howard|$/ {next}
{print $0}' >> modD0125_$1

what I am trying to do is make :

ZHV|
blah blah blah|1234|abcd|zzz,
DAN HOWARD|
blah blah blah|5678|efgh|zzz,
DAN HOWARD|
ZPT|


look like :

ZHV|
blah blah blah|1234|abcd|zzz, DAN HOWARD|
blah blah blah|5678|efgh|zzz, DAN HOWARD|
ZPT|


but when I use the above script, awk omits the
{print $0}

and I get the following :

blah blah blah|1234|abcd|zzz, DAN HOWARD|
blah blah blah|5678|efgh|zzz, DAN HOWARD|

(my header and footer is missing)

but if I use the following, everything works fine.

cat $1 | awk -F"|" '
$1 ~ /^249$/ {printf("%s|%s|%s|%s DAN HOWARD|\n",$1,$2,$3,$4);next}
$1 ~ /^ZHV$/ {print $0}
$1 ~ /^ZPT$/ {print $0}
/^DAN HOWARD|$/ {next}
/^Dan Howard|$/ {next}' >> modD0125_$1

it obviously has something to do with the ordering, but I can't see what (do pipe chars need to be escaped?)

any ideas?

thanks in advance!
John


chicken or egg first?
7 REPLIES 7
harry d brown jr
Honored Contributor

Re: awk - script sequence problem?

John,

What the .... is this:

$1 ~ /^249$/

??

Is that a ctrl-249 then a $ sign ??

live free or die
harry
Live Free or Die
harry d brown jr
Honored Contributor

Re: awk - script sequence problem?


John,

The issue I guess was your listed input of

ZHV|
blah blah blah|1234|abcd|zzz,
DAN HOWARD|
blah blah blah|5678|efgh|zzz,
DAN HOWARD|
ZPT|

Which contains no "249"'s.

live free or die
harry
Live Free or Die
Jean-Louis Phelix
Honored Contributor

Re: awk - script sequence problem?

Hi,

My problem is that it seems to work on my system ... but your test case doesn't match your script (like first field equals 249), so perhaps I made something wrong in my program which makes it work ...

Regards.
It works for me (© Bill McNAMARA ...)
u856100
Frequent Advisor

Re: awk - script sequence problem?

Hi Harry,

I was using $1 ~ /^249$/ to match the first field ($1) of all rows that start (^) with the numeric 249 only (hence the (^ ....... $) to ensure I only get fields that are 249 alone and not 2491234, etc.

Jean,

Hmm strange, we are on HP-UX 11.00. I am a bit confused as to why it isn't working, It seems to only work one way on my machine (HP 'N' class), nevermind, at least I have a work around,

thanks for your responses guys
John
chicken or egg first?
harry d brown jr
Honored Contributor

Re: awk - script sequence problem?

John,

If I change your "blah blah blah"'s to 249, then the script works:

# ./tyu uyt
ZHV|
249|1234|abcd|zzz, DAN HOWARD|
249|5678|efgh|zzz, DAN HOWARD|
ZPT|
# cat ./tyu uyt
#!/usr/bin/ksh
#
cat $1 | awk -F"|" '
$1 ~ /^249$/ {printf("%s|%s|%s|%s DAN HOWARD|\n",$1,$2,$3,$4);next}
/^DAN HOWARD\|$/ {next}
/^Dan Howard\|$/ {next}
{print $0}'
ZHV|
249|1234|abcd|zzz,
DAN HOWARD|
249|5678|efgh|zzz,
DAN HOWARD|
ZPT|
#


live free or die
harry
Live Free or Die
u856100
Frequent Advisor

Re: awk - script sequence problem?

Jean, I see what you mean!

here is an example file :

ZHV|
249|1100019210315|20021105|DAN HOWARD.|
249|1100000348295|20021105|DAN HOWARD.|
ZPT|

cheers
John
chicken or egg first?
harry d brown jr
Honored Contributor

Re: awk - script sequence problem?

John,

that input doesn't work either.

ZHV|
249|1100019210315|20021105|DAN HOWARD.|
249|1100000348295|20021105|DAN HOWARD.|
ZPT|

??

live free or die
harry
Live Free or Die