Re: how to pass shell variable value into awk scripts (509 Views)
Reply
Frequent Advisor
MANISH PATEL_2
Posts: 56
Registered: ‎01-23-2007
Message 1 of 10 (509 Views)
Accepted Solution

how to pass shell variable value into awk scripts

Hi Guys,

I want to pass x.sh variable value(userid) into my.awk scripts in "s4" value.
I excute following my.awk file from x.sh. And "s4" field passed the "userid" value into
TMPTRANSACT.TXT file.

----------------------
x.sh
----------------------
userid="428"
cd /appl/outgoing/incoming
awk -f my.awk TRANSACT.DAT > "TMPTRANSACT.TXT"

----------------------
my.awk
----------------------
{
s1=substr($0,1,30)
s2=" 1"
s3=substr($0,31,16)
s4=--->Here i want to get x.sh variale "userid" value.
s5=substr($0,47,7)
s6="E"
s7=substr($0,54,5)
s8=substr($0,59,5)
s9=substr($0,64,5)
s10="E"
s11=" 0.000E 0.00 000 000000000"
print s1 s2 s3 s4 s5 s6 s7 s8 s9 s10 s11
}
----------------------------------------

Can you guide me how to get shell scripts value into awk scripts.

Thanks
Manish
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 2 of 10 (509 Views)

Re: how to pass shell variable value into awk scripts

In x.sh do...

awk -v uid=$userid -f my.awk TRANSACT.DAT > "TMPTRANSACT.TXT"
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 3 of 10 (509 Views)

Re: how to pass shell variable value into awk scripts

Use the -v awk option. Multiple -v options are allowed to allow you to pass n options to awk.

awk -v "myvar1=${MYVAR1}" -v "mickey=Mickey Mouse" -v myvar2=${USERNAME}" -f my.awk < infile > outfile

It's getnerally a good idea to enclose the entire -v "myvar=${X}" string in double quotes to preserve whitespace. In this example, the awk variable is "myvar" and it gets assigned whatever the current value of the shell variable $X happens to be.
If it ain't broke, I can fix that.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 10 (509 Views)

Re: how to pass shell variable value into awk scripts

Hi Manish:

Modern versions of 'awk' support the '-v' option for passing variables. Older versions insist the the external assignment is made as part of the argument list passed. Compare:

# awk -v VAR="world" 'BEGIN{print "hello",VAR}' /dev/null

# awk -v VAR="world" 'END{print "hello",VAR}' /dev/null

# awk 'BEGIN{print "hello",VAR}' VAR="world" /dev/null

# awk 'END{print "hello",VAR}' VAR="world" /dev/null

Note particularly the diffence in output in the third form. The advanage to the '-v arg=value' form is that variable assignment occurs even before the 'BEGIN' rules are executed.

Regards!

...JRF...
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 10 (509 Views)

Re: how to pass shell variable value into awk scripts

Hi (again):

I would also note that it is appropriate and courteous to consider this guideline:

https://forums1.itrc.hp.com/service/forums/helptips.do?#28

Regards!

...JRF...
Frequent Advisor
MANISH PATEL_2
Posts: 56
Registered: ‎01-23-2007
Message 6 of 10 (509 Views)

Re: how to pass shell variable value into awk scripts

Hi Sandman

Thanks for your help. Infect all other scripts also work file.

Can you pls help me how can i check the o value and replace with 1 through awk.

Through above my.awk scripts i want to check field "s5" (new position of this field is from 55 to 59). I want to replace this field with 0.1 if this field value is 0.0.
Can you please let me know how can i write script. If possible can you also let me know in sed.

Thanks
Manish
Honored Contributor
Oviwan
Posts: 692
Registered: ‎11-29-2005
Message 7 of 10 (509 Views)

Re: how to pass shell variable value into awk scripts

Hey

Check the sub function:

sub(regexp, replacement [, target])

example
str = "water, water, everywhere"
sub(/at/, "ith", str)

Regards
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 8 of 10 (509 Views)

Re: how to pass shell variable value into awk scripts

>Through above my.awk scripts i want to check field "s5" (new position of this >field is from 55 to 59). I want to replace this field with 0.1 if this field value is >0.0. Can you please let me know how can i write script. If possible can you >also let me know in sed.

I am not sure I understand your requirement? Could you please provide an example of what you are looking to get done?

~thanks
Acclaimed Contributor
Dennis Handly
Posts: 25,296
Registered: ‎03-06-2006
Message 9 of 10 (509 Views)

Re: how to pass shell variable value into awk scripts

>Through above my.awk scripts I want to check field "s5" (new position of this field is from 55 to 59). I want to replace this field with 0.1 if this field value is 0.0.

If you are talking about the s5 variable above that you have extracted from 47 to 53, you can just use:
if (s5 == 0.0) s5 = 0.1

If you print this numeric value, you may have to use printf to have a fixed field width. Otherwise you can treat s5 as a character string.

>If possible can you also let me know in sed.

If you have " 0.0" in columns 55..59, you could use this sed script:
$ sed -e 's/^\(\{54\}\) 0\.0\/\1 0.1/' file

(There are two spaces before the "0" for a field width of 5.)
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 10 of 10 (509 Views)

Re: how to pass shell variable value into awk scripts

Hi Manish:

> Through above my.awk scripts i want to check field "s5" (new position of this >field is from 55 to 59). I want to replace this field with 0.1 if this field value is 0.0. Can you please let me know how can i write script.

Perl's 'substr' can be used as an lvalue or can take a fourth argument as a replacement string. Hence you could do, by example:

# echo "abc0.0def"|perl -pe 'substr($_,3,3,"0.1") if substr($_,3,3)=="0.0"'

Perl counts 0-relative. Hence, here, every line that contains "0.0" in positions 3-5 is replaced by "0.1".

Regards!

...JRF...
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.