ksh variable pass to awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh variable pass to awk
# 1  
Old 05-09-2009
ksh variable pass to awk

I'm trying to store the response from a nawk command inside of a ksh script. The command is:
text=$(nawk -F: '$1 ~ /${imgArray[$i]}/ {print $2}' ${etcDir}/captions.txt)

From what I can tell, the imgArray variable is not being expanding when it is inside the single quote ('). Is there something I need to do to escape this to make it work?
# 2  
Old 05-09-2009
Try putting a "'" on either side of any variables in a nawk or awk command like this:

Code:
text=$(nawk -F: '$1 ~ /'${imgArray[$i]}'/ {print $2}' ${etcDir}/captions.txt)

# 3  
Old 05-09-2009

Code:
text=$(nawk -F: '$1 ~ /'"${imgArray[$i]}"'/ {print $2}' ${etcDir}/captions.txt)

Note the double quotes around the variable; they are needed in case there is whitespace inside its value.

A more normal method is:

Code:
text=$(nawk -F: -v var="${imgArray[$i]}" 'index($1,var) {print $2}' ${etcDir}/captions.txt)

# 4  
Old 05-10-2009
I think that you need to use the execution "quotation marks", for example:

e=`awk -F: '{ print $1 }' test.txt`
# 5  
Old 05-10-2009
Thanks!

The second one from cfajohnson worked prefect. Thanks everyone for their help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh script trying to pass a variable to edit a file

I'm trying to create a ksh script that will ask the user for the port number. $PORT1 is the variable I want to use that will contain whatever numbers the user inputs. The script would edit ports.txt file, search and delete "./serv 110.1.0.1.$PORT1 200;=3" . So if the user types 50243 then the... (5 Replies)
Discussion started by: seekryts15
5 Replies

2. UNIX for Dummies Questions & Answers

Pass shell Variable to awk

Hello, May i please know how do i pass the shell variable to awk expression in the below script. It is returning null #!/bin/bash UNINUM=720922 UNINUM_DESC=`awk -F'|' -v UNINUM=$2 '/UNINUM/ {print $4}' datafile` echo $UNINUM_DESC datafile 4|First|720194|asdasdad 4|First|720735|asdasdsa... (8 Replies)
Discussion started by: Ariean
8 Replies

3. Shell Programming and Scripting

pass an awk variable to kill

Does anyone know of a way to do something similar to this with awk and kill? I want to create the variable in awk and pass that variable to kill. ps -ef | grep -i chromium | awk '{$2=x}' | kill -9 $x 2>/dev/null (9 Replies)
Discussion started by: cokedude
9 Replies

4. UNIX for Dummies Questions & Answers

How to pass a variable from shell to awk

I know this topic has been dealt with previously, but the solutions I've seen don't work for me apparently. I need to pass a variable defined in the shell to one in awk: $ echo $var1 3 $ cat aaa aaa 1 bbb 2 ccc 3 ddd 4 eee 5I've tried this, without success: $ awk... (2 Replies)
Discussion started by: metaltree
2 Replies

5. Shell Programming and Scripting

AIX ksh: how to pass variable to host shell

I have a script that "runs" a script. For example: ./runscript.ksh pcnmc01.ksh runscript puts pcnmc01.ksh into the background with log output going to the logfile. After executing the command, I get this output: Running script in the background: pcnmc01.ksh Logfile:... (2 Replies)
Discussion started by: Eben Yong
2 Replies

6. 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

7. 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

8. 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

9. Shell Programming and Scripting

How to pass variable to SQLPLUS in a ksh script?

Hi, I am writing a ksh script which will use sqlplus to run a sql and pass 2 variables as the SQL request. In the ksh script, I have 2 variables which are $min_snap and $max_snap holding 2 different numbers. Inside the same script, I am using SQLPLUS to run an Oracle SQL script,... (6 Replies)
Discussion started by: rwunwla
6 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