Computing.Net > Forums > Unix > help with awk

help with awk

Reply to Message Icon

Original Message
Name: vishamr2000
Date: April 1, 2005 at 02:16:46 Pacific
Subject: help with awk
OS: Unix
CPU/Ram: Intel/256
Comment:

Hi to all,
I wrote sth like this:
#!/bin/sh
x="192.168.10.10"
awk '$1 == "'$x'" { y=$2} ' myfile

echo $y
##########

the echo cmd does not display anything. What am I doing wrong.


Report Offensive Message For Removal

Response Number 1
Name: gurubit
Date: April 1, 2005 at 05:15:11 Pacific
Subject: help with awk
Reply: (edit)

What do you want to do with the script ?


Report Offensive Follow Up For Removal

Response Number 2
Name: Jim Boothe
Date: April 1, 2005 at 07:30:19 Pacific
Subject: help with awk
Reply: (edit)

Variables within awk are separate from shell variables. Your code does establish the y variable within awk, but that has no impact on the shell. You need to make your awk code print the desired value, then you can capture the awk output into a shell variable:

y=$(awk '$1=="'$x'"{print $2;exit} ' myfile)

The $() is ksh or bash syntax (or you can use backquotes).

As coded, awk will exit after printing the first qualifying value. If you want multiple values fed into y, then remove the exit.


Report Offensive Follow Up For Removal

Response Number 3
Name: vishamr2000
Date: April 3, 2005 at 22:47:21 Pacific
Subject: help with awk
Reply: (edit)

Hi to all,

That's what i wanted jim..many thx for your help.

To Rajesh: I wanted to use the $1 and $2 from the result of applying the awk cmd to a file, in a shell script. Thx for your asking..

Thx again..

Regards,
Visham


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: help with awk

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software