cbauw
September 9, 2008, 9:48pm
1
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
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:
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.
Are you saying your file is a single line containing the string "20080831"?
Annihilannic, I noticed the $ also. But for some reason "getline RunDate; print $RunDate" actually outputs "20080831"
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.
cbauw
September 10, 2008, 12:09am
6
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.
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?