Passing a parameter to AWK

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing a parameter to AWK
# 1  
Old 07-08-2011
Passing a parameter to AWK

Hi All,

I am trying to pass a parameter to AWK on my KSH shell prompt as below.

echo $var1
awk -v var2=${var1} '{print var2}' testfile.txt

I am passing the input file (testfile) to awk to get some o/p. It is having 10 records.

When I run AWK, it is throwing the following errors
awk: syntax error near line 1
awk: bailing out near line 1

I googed this forum, the syntax i am using is correct, but dont know why it is failing in this case.

My OS is SOLARIES,is there any difference syntax for AWK for passing parameters to AWK on sun solaries?

Can any one please help.

---------- Post updated at 12:46 AM ---------- Previous update was at 12:38 AM ----------

Hi All,

It got solved. The correct syntax is

awk '{print var2}' var2="$var1" test111.xls

I think there is syntax change in Sun solaries from BASH it seems.

In bash, generally we pass -v option before print, but in solaries, this is working if I use -v option after print.
# 2  
Old 07-08-2011
Don't use awk with Solaris. /usr/bin/awk is a legacy binary which is there only to provide compatibility with ancient scripts that might still wander around.

Instead, use either nawk or /usr/xpg4/bin/awk.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Loop over awk or passing parameter

I wrote this script which works well when I manually input 55518622 and 1 but I need this script to be generic and loop over the following table awk '$4>(55518622-500000) && $4<(55518622+500000)' chr1_GEN2bim | awk 'BEGIN {min=1000000000; max=0;}; {\ if($4<min && $4 != "") min = $4; if($4>max... (8 Replies)
Discussion started by: fat
8 Replies

2. UNIX for Dummies Questions & Answers

Passing shell script parameter value to awk command in side the 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 |... (1 Reply)
Discussion started by: Sarita Behera
1 Replies

3. Shell Programming and Scripting

Passing parameter to awk command

Hi, I have a situation where I need to create a SQL statement using Unix script for each value in first field (ID). The file looks like this. Id,TARGET_FIELD_NAME,SOURCE_FIELD_NAME 1,Test_Rate,Field1 1,Test_Factor,Field2 1,Test_Size,Field3 2,Test_Rate,Field4 2,Test_Factor,Field5... (3 Replies)
Discussion started by: kiranredz
3 Replies

4. Shell Programming and Scripting

Passing parameter more than 9

Hi, I've written a script where eleven parameter to be passed from command line which is inserting into an oracle table, it is working but the tenth and 11th parameter are not accepting as given it is referring to 1st parameter. HERE IS THE SCRIPT #!/bin/ksh #set -o echo $*... (4 Replies)
Discussion started by: sankar
4 Replies

5. Shell Programming and Scripting

Passing parameter to script, and split the parameter

i am passing input parameter 'one_two' to the script , the script output should display the result as below one_1two one_2two one_3two if then echo " Usage : <$0> <DATABASE> " exit 0 else for DB in 1 2 3 do DBname=`$DATABASE | awk -F "_" '{print $1_${DB}_$2}` done fi (5 Replies)
Discussion started by: only4satish
5 Replies

6. Shell Programming and Scripting

Error passing parameter in "sub" command in awk

I have to replace the pattern found in one file in another file with null/empty "" if found on the fields 3 or 4 ONLY File 1 ==== 10604747|Mxdef|9999|9999|9999|2012-03-04 00:00:59 10604747|Mcdef|8888|9999|8888|2012-03-04 00:00:59 . . . File 2 ==== 9999 8888 . . . Expected... (7 Replies)
Discussion started by: machomaddy
7 Replies

7. Shell Programming and Scripting

Passing parameter in sed or awk commands to print for the specific line in a file

Hi, I am trying to print a specific line in a file through sed or awk. The line number will be passed as a parameter from the previous step. My code looks as below. TEMP3=`sed -n '$TEMP2p' $FILEPATH/Log.txt` $TEMP2, I am getting from the previous step which is a numerical value(eg:3). ... (2 Replies)
Discussion started by: satyasrin82
2 Replies

8. Shell Programming and Scripting

Parameter Passing problem

Hi All, I developed a KSH script which will accept two parameters as input. These two parameters are some directories paths. In the script i am validating the number of paramaters it received as below #-------------------------------------- # Check Command line arguments... (8 Replies)
Discussion started by: Raamc
8 Replies

9. Programming

Passing parameter to makefile?

Hi, How to pass parameter to makefile? Please let me know if any one knows and also please put an example of makefile with this feature. thanks, Manju. (3 Replies)
Discussion started by: manju_p
3 Replies

10. Shell Programming and Scripting

parameter passing

Hallo everyone, This is my problem below: /home/cerebrus/pax=>vat ksh: vat: not found /home/cerebrus/pax=>cat #!/bin/ksh set -x bdf|grep appsdev|awk '{ print $5 }'> class3 dd={cat class3} echo $dd /home/cerebrus/pax=> /home/cerebrus/pax=>./ + bdf +... (8 Replies)
Discussion started by: kekanap
8 Replies
Login or Register to Ask a Question