PASS parameter to AWK


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers PASS parameter to AWK
# 1  
Old 05-14-2004
Question PASS parameter to AWK

Hi,

Can i pass a parameter(not a file name) as a parameter to a awk program?

eg;

$awk -f test 1 2 3

here test is the filename...and 1,2,3 are the i/p parameters?

thank you:-)
# 2  
Old 05-14-2004
Parameters passed to AWK are stored in the ARGV array, which has ARGC elements.

Example: suppose foo.awk contains
Code:
BEGIN {
   for ( i = 0; i < ARGC; i++ )
   {
       print ARGV[i]
   }
}

Then executing
Code:
awk -f foo.awk 1 2 3

would print
Code:
awk
1
2
3

ARGV[0] is the awk command itself, and the other ARGV elements are the parameters passed.

Peace,
ZB
http://www.zazzybob.com
# 3  
Old 05-14-2004
You can also assign variables on the command line, e.g....

awk -v a=1 -v b=2 -v c=3 -f test.awk

The -v option means that the variables are assigned before the BEGIN section.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

Unable to pass shell script parameter value to awk command in side the same script

Variable I have in my shell script 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 -F'~' ''$2 == "$id"' {print $0}' > $new I could see value of $id is not passing to the awk... (0 Replies)
Discussion started by: Ashunayak
0 Replies

2. Shell Programming and Scripting

Pass parameter

Hi, I have following for loop , please let me know how to get ${TXP_EXT_TABLE_${i}_SQL} parameter with 1DAY and 7DAY values. for i in 1DAY 7DAY do ${NZSQL_DIR}/nzsql -h ${HOST} -time -v ON_ERROR_STOP=1 -f ${SQL_DIR}/${TXP_EXT_TABLE_${i}_SQL} > ${TMP_LOG_FILE} 2>&1 done ... (4 Replies)
Discussion started by: sandy162
4 Replies

3. Shell Programming and Scripting

Pass value from file to parameter

Hi Guys, I have a file in the format Parmater=value. I want to read the value and pass it to corresponding Variable. The Parameter file is as follows Number=23 Text1=mango Text2=yup 'Number' value needs to be read and passed to ID variable. Also, 'Text1' value needs to be passed to... (9 Replies)
Discussion started by: mac4rfree
9 Replies

4. UNIX for Dummies Questions & Answers

How to pass the parameter value to a... ?

Hello I have a simple code like this one: #!/bin/ksh VER=$1 cat /usr/text | while read line do echo $line done Let's say $1=1.0.0 and the contents of text is: abcd.cfg asdf I would like the output to be like this abcd1.0.0.cfg asdf1.0.0 I am thinking of passing the... (5 Replies)
Discussion started by: khestoi
5 Replies

5. Shell Programming and Scripting

awk pass $LOGDIR parameter

hi guys i need your help , i wrote one script which is as below #!/bin/ksh ########################################################### LOGDIR=/export/home/xyz/logs EMAILFile=$LOGDIR/xxs_email.log BOX=$(uname -a | awk '{print $2}') awk '{if ($4 >= 30) {print $1 " " $3 " HAS LAG of "... (1 Reply)
Discussion started by: tapia
1 Replies

6. Shell Programming and Scripting

Pass input and output file as parameter to awk script

Hi, i am new to awk. I am using csv2pipe script(shown below) BEGIN { FS=SUBSEP; OFS="|" } { result = setcsv($0, ",") print } # setcsv(str, sep) - parse CSV (MS specification) input # str, the string to be parsed. (Most likely $0.) # sep, the separator between the values. # #... (6 Replies)
Discussion started by: bhaskarjha178
6 Replies

7. Shell Programming and Scripting

How to pass a parameter

Hi all, How to pass a parameter from a oracle pl/sql procedure parameter to shell environment and use it? (1 Reply)
Discussion started by: megh
1 Replies

8. Shell Programming and Scripting

Pass parameter into script

I would like to write a scirpt a.sh that it first checks the first parameter of the input. If it fulfill some condition ,then run an executable program b by using all the parameter. ie. > ./a.sh 10 20 30 40 50 Then a.sh first checks the first parameter, 10, if it mathes the requirement, then... (2 Replies)
Discussion started by: alfredo
2 Replies

9. Shell Programming and Scripting

How to pass one parameter from one column to another column in AWK

I am having scenario where input file is containing text like below ----------------------------------- Name,x.y.z.i.j.k,p.q.r.s.t.k,y.d.f.u.g.h,e.r.t.yu.t.u,...... Place,Bangalore,hyderabad,Chennai,Delhi,............ I need to read and put it in file in a column structure ... (1 Reply)
Discussion started by: prasanta jena
1 Replies
Login or Register to Ask a Question