1833873 Members
2787 Online
110063 Solutions
New Discussion

Re: AWK variables

 
SOLVED
Go to solution
u856100
Frequent Advisor

AWK variables

Hi all,

does anyone know how to reference a variable in awk, i.e.

I want to pick up a specified column from row1 and use it for the output of row2

say we have :
header|1|
a|b|c|d|e|f|
footer|0|

and I wanted to change this to :
header|1|
a|b|c|d|e|f|
footer|1|

but the new footer variable is picked up from the header numeric (in this case 1)

so the following :
header|5|
a|b|c|d|e|f|
footer|2|

needs to be :
header|5|
a|b|c|d|e|f|
footer|5|

thanks in advance gurus!

John
chicken or egg first?
10 REPLIES 10
James R. Ferguson
Acclaimed Contributor
Solution

Re: AWK variables

Hi John:

'substr' is suited for this. For example:

# echo "header|1|" | awk '{X=substr($0,8,1);print X}'

...would print:

1

Regards!

...JRF...
Fred Martin_1
Valued Contributor

Re: AWK variables

Highly recommend the book "The AWK Programming Language" by Aho, Kernighan, Weinberger. One of the smallest, yet perfectly consise manuals I've ever seen. Addison-Wesley Publishing Company.
fmartin@applicatorssales.com
curt larson_1
Honored Contributor

Re: AWK variables

cat yourfile | awk -F"|" '
/header/ {x=$2;print $0;next;}
/footer/ {sub($2,x);print $0;next;}
{print $0;}'
u856100
Frequent Advisor

Re: AWK variables

Hi Curt,

I can't get this to work as its trying to pick up the $2 entry for the footer string and not the header string

I know by default, sub() uses $0 if a third parameter is ommitted.
chicken or egg first?
Andreas Voss
Honored Contributor

Re: AWK variables

Hi,

here a little correction to curt's answer:

cat yourfile | awk -F"|" '
/header/ {x=$2;print $0;next;}
/footer/ {sub("|" $2 "|","|" x "|", $0);print $0;next;}
{print $0;}'

Regards
Robin Wakefield
Honored Contributor

Re: AWK variables

Hi John,

This works for me, and you cut out a process by not using cat/pipe:

awk -F\| '/header/{x=$2}/footer/{sub($2,x)}{print}' yourfile

rgds, Robin
u856100
Frequent Advisor

Re: AWK variables

all, thanks again for your cotinous replies.

I can simulate curts example, but I am having problems applying to a specific case. The full extent of what I am trying to do is as follows :

original file
-------------
ZHD|MDN0980268|D0215001|R|EMEB|X|BGAS|20030220114355|FORMF||OPER|
490|1100009199347|0|0|1|||||||METER MOVED OUTSIDE
ZPT|3|1111379456

I want to change into this
--------------------------
ZHV|MDN0980268|D0215001|R|EMEB|X|BGAS|20030220114355|FORMF|||OPER|
490|1100009199347|0|0|1|||||||METER MOVED OUTSIDE|
ZPT|MDN0980268|1||1|20030224000001|

But the script I have :


gives me this :
ZHV|MDN0980268|D0215001|R|EMEB|X|BGAS|20030220114355|FORMF|||OPER|
490|1100009199347|0|0|1|||||||METER MOVED OUTSIDE|
ZPT|1|1||1|20030224000001|

I don't understand why the second pipe delimitted field in the last column is '1' and not 'MDN0980268'

yours in confusion
John
chicken or egg first?
u856100
Frequent Advisor

Re: AWK variables

please ignore the extra carriage returns after the first row.

thanks
chicken or egg first?
u856100
Frequent Advisor

Re: AWK variables

might be useful if I included the script (doh!)

for i in `ls n098*`
do
awk -F"|" '
$1 ~ /^ZHD$/ {x=$2;printf("ZHV|%s|%s|%s|%s|%s|%s|%s|%s|%s||%s|\n",$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)}
$1 ~ /^ZPT$/ {print "ZPT|"sub($2,x)"|1||1|20030224000001|"}
$1 ~ /^490$/ {print $0"|"}' $i > /data/archive2/jb/formfill_new/$i
done


chicken or egg first?
Robin Wakefield
Honored Contributor

Re: AWK variables

Hi John,

The "1" that you are seeing is the return value from the sub function. You should do the substitution before printing, or simply put x in the print statement directly e.g.

awk -F"|" '
$1 ~ /^ZHD$/ {x=$2;printf("ZHV|%s|%s|%s|%s|%s|%s|%s|%s|%s||%s|\n",$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)}
$1 ~ /^ZPT$/ {print "ZPT|"x"|1||1|20030224000001|"}
$1 ~ /^490$/ {print $0"|"}'

rgds, Robin