Using variable in NAWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using variable in NAWK
# 1  
Old 03-22-2009
Question Using variable in NAWK

I'm trying the following script using nawk and failed to call variable inside the script.

echo "Enter the absolute path of XML location for respective billcycle"
read xml_location
grep "string to find:" abcd.log|cut -d '/' -f12|nawk '{print $xml_location $1}' > redirected_log.log

Please suggest how can i use the variable $xml_location inside nawk

Thanks
Nadvi
# 2  
Old 03-22-2009
Needless use of grep and cut with awk. I don't know how your file looks like but something like this should be suffice:

Code:
nawk -F"/" '/string to find/{print var $12}' var="$xml_location" abcd.log > redirected_log.log

In your solution the use of the variable should look like:

Code:
grep "string to find:" abcd.log|cut -d '/' -f12|nawk '{print var $1}' var="$xml_location" > redirected_log.log

Regards
# 3  
Old 03-22-2009
Not being a scripting guru I would have done:

Code:
grep "string to find:" abcd.log|cut -d '/' -f12|nawk '{print '$xml_location' $1}'

but the nawk solution is of course a lot more elegant.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Substitute variable inside nawk

Hi, I need to set "prd" in the below command to a unix variable nawk '/^#/ {next} FNR==NR {prd;next} !($0 in prd)' So, this is what i did fname=prd // unix shell variable nawk -v fname=$fname '/^#/ {next} FNR==NR {fname;next} !($0 in fname)'But the value of fname i.e "prd" is not... (8 Replies)
Discussion started by: mohtashims
8 Replies

2. Shell Programming and Scripting

How to pass nawk variable to shell within the same script?

Hi All, I tried googling but so far no luck, can someone tell me how pass the variable value used inside the nawk command to shell. In the below script i get the value of $c (without color: Total Executed: " c ") but the printf which is outside the nawk command doesn't print the value or it... (4 Replies)
Discussion started by: Optimus81
4 Replies

3. Shell Programming and Scripting

issue with nawk while using variable assignment

#ifconfig -a | nawk '/1.1.1.1/{print}' inet 1.1.1.1 netmask xxxxxxxxx broadcast 0.0.0.0 If i assign the ip to a variable and search for the variable nothing gets printed!! # ifconfig -a | nawk -v ip=1.1.1.1 '/ip/{print}' I am not able to understand why this is happening! (6 Replies)
Discussion started by: chidori
6 Replies

4. Shell Programming and Scripting

Nawk variable question

Hi, I'm trying to replace a string with the contents of a file.. I found the below thread that shows how to do this but is there any way to use a variable as the file name in the nawk command?.. When I try I get an error saying while read groupsvar do ... (5 Replies)
Discussion started by: Jazmania
5 Replies

5. Shell Programming and Scripting

Help nawk change external variable

Hello, I have external variable rownumber, I am processing files within a loop and I would like to keep incrementing rownumber. What is happening is inside nawk section it passes rownumber but it never gets updated. I want to update rownumber inside the nawk, I would appreciate your help ... (6 Replies)
Discussion started by: srattani
6 Replies

6. Solaris

NAWK - setting a variable to the highest value

Hi, new to nawk so not sure how this works, I have a file with three values 23:36:18 - i need to set a variable called SCORE with the highest value from the file (i.e. 36) using nawk can anyone help? thanks (1 Reply)
Discussion started by: Pablo_beezo
1 Replies

7. Shell Programming and Scripting

Passing shell variable to NAWK

I am trying to make a simple script in which i take input from shell and then forward the value to nawk (BEGIN). but when i run below mention script so it give no output. echo "Enter TRUNK GROUP:" read TGR cat /omp-data/logs/5etr/080422.APX | nawk -F"|" -v P=$TGR ' BEGIN { TG=P;... (1 Reply)
Discussion started by: wakhan
1 Replies

8. Shell Programming and Scripting

How to make nawk to set a shell variable

Below is the content of the file I am parsing bash-2.03$ cat rakesh cpio: Cannot write "reloc/lib/gcc-lib/i386-pc-solaris2.7/2.95.2/gmon.o", errno 28, No space left on device cpio: Cannot write "reloc/lib/gcc-lib/i386-pc-solaris2.7/2.95.2/include/README", errno 28, No space left on device... (1 Reply)
Discussion started by: rakeshou
1 Replies

9. UNIX for Dummies Questions & Answers

Passing a for loop variable into nawk

After searching through books and the internet for days I can't seem to find an example of this. I'm trying to pass a variable from a for loop into nawk but can't seem to get all the syntax right. my script (thanks to anbu23 for nawk help) is this: for customers in `cat customers.txt` do... (3 Replies)
Discussion started by: Ant1815
3 Replies

10. Shell Programming and Scripting

is it possible to pass external variable values to nawk?

Dear friends, please tell me how to pass the external variable values to the nawk command. length=`expr $len2 - $len1` i need to pass $length to following nawk command as mentioned below. nawk '{if((x=index($0,"W/X"))>0){id=substr($0,x, $length);print x;print id;}}' filename1 but I am... (1 Reply)
Discussion started by: swamymns
1 Replies
Login or Register to Ask a Question