- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: how to pass shell variable value into awk scri...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2007 09:28 AM
тАО05-29-2007 09:28 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2007 09:31 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2007 09:44 AM
тАО05-29-2007 09:44 AM
Re: how to pass shell variable value into awk scripts
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2007 09:55 AM
тАО05-29-2007 09:55 AM
Re: how to pass shell variable value into awk scripts
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2007 09:59 AM
тАО05-29-2007 09:59 AM
Re: how to pass shell variable value into awk scripts
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-30-2007 09:03 AM
тАО05-30-2007 09:03 AM
Re: how to pass shell variable value into awk scripts
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-30-2007 09:14 AM
тАО05-30-2007 09:14 AM
Re: how to pass shell variable value into awk scripts
Check the sub function:
sub(regexp, replacement [, target])
example
str = "water, water, everywhere"
sub(/at/, "ith", str)
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-30-2007 09:48 AM
тАО05-30-2007 09:48 AM
Re: how to pass shell variable value into awk scripts
I am not sure I understand your requirement? Could you please provide an example of what you are looking to get done?
~thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-30-2007 11:01 PM
тАО05-30-2007 11:01 PM
Re: how to pass shell variable value into awk scripts
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.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-31-2007 01:50 AM
тАО05-31-2007 01:50 AM
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.
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...