How to pass a Awk variable to another script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to pass a Awk variable to another script
# 1  
Old 09-09-2008
Data How to pass a Awk variable to another script

Read parameter from a text file with one line which stored the date value like 20080831; below is the awk command I used

gawk -F, "{getline RunDate;print $RunDate" text file

When print $RunDate, it display 20080831

Would like to pass this variable to another script to use but not successful.

Please advice. Thanks
# 2  
Old 09-09-2008
Are you sure it's print $RunDate and not print RunDate? You don't normally prefix awk variables with $ unless you are referring to field numbers.

You can assign the output to a shell variable using something like:

Code:
rundate=$(gawk -F, 'BEGIN { getline; print $1}' textfile)

I used $1 because I'm guessing that RunDate is the first field in the input file.
# 3  
Old 09-09-2008
Are you saying your file is a single line containing the string "20080831"?
# 4  
Old 09-09-2008
Annihilannic, I noticed the $ also. But for some reason "getline RunDate; print $RunDate" actually outputs "20080831"
# 5  
Old 09-09-2008
Normal awks complain, but it seems gawk treats $var where var contains a string the same way as $0. Interesting!

In which case, the OP may as well just use rundate=$(cat textfile) or read rundate < textfile.
# 6  
Old 09-10-2008
Hammer & Screwdriver

Thanks for the quick response. But I am very new on the AWK and script writing. Yes, the file only contain the date. The script is running on Window. What will be the exact syntax? I am trying to read the file, get the date and pass the date as a variable to execute for a program.
Thanks for your help.
# 7  
Old 09-10-2008
So you're running this from the Windows command prompt? If so, you should mention that when you are posting on forums on Unix.com. :-)

The advice I have given so far will only work on Unix or a Unix-like shell such as Cygwin.

What kind of script is the other one you need to pass the variable to?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass variable from awk script to shell?

Hi, Please need to print the Rej variable outsite the awk script which is given below...please advised how to achieve it. #!/bin/bash echo "Enter DMU Pipe delimited File name for the Feed to be validated" read DMU_File echo "Enter Pre-DMU File name for the Feed" read Predum_file ... (3 Replies)
Discussion started by: pelethangjam
3 Replies

2. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

3. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

4. Shell Programming and Scripting

pass variable from awk to shell script

Hello Experts, Actually I was searching for a solution here in this forum , but didn't get what exactly I want . Is this possible to do in awk ? I am trying to do some thing like below in ksh script . Upto my knowledge I can pass shell script to awk with "-v " option. But I... (3 Replies)
Discussion started by: user_prady
3 Replies

5. Shell Programming and Scripting

Is it possible to pass variable from awk to shell script

Hello experts, can I return a value from gawk to a shell script ? My script as follows, #Here I want the num value to shell script so that I can use later gawk ' { split($0,num,","); print num }' gawk -v no=$number '{print no}' file1 ... (3 Replies)
Discussion started by: user_prady
3 Replies

6. UNIX for Dummies Questions & Answers

How to pass Shell script variable to awk

Hi, I have a shell script with an ambedded awk script. i need to pass a script variable to the awk script. Please help. Thanks in advance Himani (3 Replies)
Discussion started by: HIMANI
3 Replies

7. Shell Programming and Scripting

Pass script variable value to AWK

HI all, some more mistery about AWK, I hope you can help me out: 1) I have a normal ksh script and sometime I call awk command. I set some variables in the script and I would like to use them up within AWK as well. Unfortunately AWK seems to forget all the variable values outside of its own... (1 Reply)
Discussion started by: BearCheese
1 Replies

8. UNIX for Dummies Questions & Answers

How do I pass a variable to awk?

I have an awk statement where I Need to pass an environment variable but I cannot get it to work: My evironment varible examples below: $FILE1=/dev/fs/file.new $FILE2=/dev/fs/file.old Code below: awk -F"|" ' BEGIN { while( getline < "$FILE1" ) { arr=1 } } arr != 1 { print } '... (12 Replies)
Discussion started by: eja
12 Replies

9. Shell Programming and Scripting

How to pass a variable to Awk ?

I am trying to pass 2 shell variable's ("START" and "END") define earlier in the script to this awk statement, but i can't seem to pass it on. PLs help. set START = xxxx set END = yyyy set selected_file = `awk '/$START/,/$END/' filename` (24 Replies)
Discussion started by: Raynon
24 Replies

10. UNIX for Dummies Questions & Answers

pass variable to awk

i would like to pass a variable to awk wherein the variable comes from external loop. i tried this... let x=0 until test $x -eq 32 do cat file | awk '{ print $1 , "Number" , $($x) }' >> output done thanks, (4 Replies)
Discussion started by: inquirer
4 Replies
Login or Register to Ask a Question