passing variables to awk from ksh script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting passing variables to awk from ksh script
# 1  
Old 08-11-2005
passing variables to awk from ksh script

I'm trying to write a ksh script that uses awk, but I want to pass variables to awk. For example (not working):

if [[ -n $1 ]];then
searchstr=$1
lsof -i | awk '{if($9~/SEARCHSTR/) print $2} SEARCHSTR=$searchstr'
else
echo "usage: $0 <search string>"
fi

I tried several options. Is it posisble to do this without writing a sepoerate awk script and load it with: awk -f script.awk var=$value

I prefer to have all the code in the same script.

[The script tries to get all pid's assosciated with a certain portnumber.]
# 2  
Old 08-11-2005
Code:
lsof -i | awk -v srch=$searchstr '{if(index($9,srch)>0) print $2} '

I wasn't positive what you need - I just hacked together something.
Short answer: Use -v
# 3  
Old 08-11-2005
I was looking for someting like this:


if [[ -n $1 ]];then
lsof -i | awk '{if($9~/'$1'/) print $2}'
else
echo "usage: $0 <search string>"
fi


Just unqoute $1 in awk and it gets expanded by the shell and it also works.

Thanbks for the help.
# 4  
Old 08-11-2005
rein, just remove the \ around SEARCHSTR and move the last ' :
Code:
if [[ -n $1 ]];then
searchstr=$1
lsof -i | awk '{if($9~SEARCHSTR) print $2}' SEARCHSTR=$searchstr
else
echo "usage: $0 <search string>"
fi

The variable SEARCHSTR is avalaible in all your awk code except in the BEGIN pattern.

If you want to use the variable in BEGIN, use the following syntax (with a little variation in the awk code) :
Code:
if [[ -n $1 ]];then
lsof -i | awk -v SEARCHSTR=$1 '$9~SEARCHSTR {print $2}' 
else
echo "usage: $0 <search string>"
fi

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh While Loop - passing variables to functions

I'm reading a text file using a while loop but when I call a function from within this loop it exits that same iteration … even though there are many more lines in the file to be read. I thought it was something to do with the IFS setting but it appears that a function call (when run... (3 Replies)
Discussion started by: user052009
3 Replies

2. Shell Programming and Scripting

awk - passing variables in and out

Am looking to pass some Linux environment variables into AWK , can I simply use the -v option ? awk -F: -v AHOME=$HOME '{ if {rm AHOME/file.txt a=2 } }' config.txt ... (4 Replies)
Discussion started by: alldbest
4 Replies

3. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

4. Shell Programming and Scripting

Passing variables into AWK

I'm trying to use awk to write new entries to a hosts file if they don't exist. I need to do so depending on the type of system I have. Below is what I have, but it isn't working. awk -v myip1=$IP1 myip2=$IP2 myhost1=$HOST1 myhost2=$HOST2' BEGIN { mqhost1=0; mqhost2=0; stap1=0; stap2=0; } ... (4 Replies)
Discussion started by: Boomn4x4
4 Replies

5. Shell Programming and Scripting

ksh passing to awk multiple dyanamic variables awk -v

Using ksh to call a function which has awk script embedded. It parses a long two element list file, filled with text numbers (I want column 2, beginning no sooner than line 45, that's the only known thing) . It's unknown where to start or end the data collection, dynamic variables will be used. ... (1 Reply)
Discussion started by: highnthemnts
1 Replies

6. Shell Programming and Scripting

awk issue expanding variables in ksh script

Hi Guys, I have an issue with awk and variables. I have trawled the internet and forums but can't seem to get the exactt syntax I need. I have tried using awk -v and all sorts of variations but I have hit a brick wall. I have spent a full day on this and am just going round in circles. ... (3 Replies)
Discussion started by: gazza-o
3 Replies

7. Shell Programming and Scripting

passing variables to sed in ksh

Hi, i need help passing variables to sed using ksh. My goal is to get particular data from log files. first i put a mark to the log files. echo "TEST_"`date + %m_%d_%Y_%T"` >markFile this will produce a 'markFile' which contain text like this TEST_06_01_2009_21:55:09 then i put the mark... (2 Replies)
Discussion started by: d.anggrianto
2 Replies

8. Shell Programming and Scripting

passing a variables value from the called script to calling script using ksh

How do i get the value of the variable from the called script(script2) to the calling script(script1) in ksh ? I've given portion of the script here to explain the problem. Portion of Script 1 ============= ----- ----- tmp=`a.ksh p1 p2 p3` if then # error processing fi -----... (10 Replies)
Discussion started by: rajarkumar
10 Replies

9. Shell Programming and Scripting

Passing awk Variables

I am trying to pass the results from a variable gathered from awk, however when I echo the 'PARSE' and 'SUB', the response is blank. This is my command. awk -F= '/Unit/''{ PARSE=substr($2,1,5) ; SUB=substr($2,1,1) }' inputfile.lst Is this a kind of valid attempt or am I obligated to declare... (3 Replies)
Discussion started by: gozer13
3 Replies

10. Shell Programming and Scripting

Passing Variables to Awk

Hi I have a unix shell script with an awk statement. I would like to print some of the fields of an input file. However, I would like to print them dynamically, ie by passing the literal $1 $3 into the script to define the output. I have tried the following: variable1='$1' awk... (2 Replies)
Discussion started by: Bab00shka
2 Replies
Login or Register to Ask a Question