UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old February 3rd, 2004, 05:02 AM
jojo123 jojo123 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 10 jojo123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy AWK command

Hi,
I want to write a simple command at awk.
i have a file:

a=1
b=2
c=3

i want to print c=3.

i worte the following command:

awk '/c/ {print $1,$2,$3}' filename.(it works great)

i want to write the same command,but with an argumant:

x="c"
awk '/$x/ {print $1,$2,$3}' filename.(it's not working!!!).

Please help.

Thanks,

Reply With Quote
  #2  
Old February 3rd, 2004, 08:55 AM
druuna druuna is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 137 druuna User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 37 sec
Reputation Power: 0
You need to tell awk that you want to pass a variable from the shell to awk, this can be done with awk's -v var=value option.

So your example will look like this:

X="c"
awk -v XYZ=$X '/c/ {print $1}' filename


You also use the $1, $2 and $3 incorrectly. There are no (standard) seperators between a=1, b=2 and c=3. This means that a=1 is $1 (same for b=2 and c=3).

Hope this helps.

Reply With Quote
  #3  
Old February 4th, 2004, 04:19 AM
jojo123 jojo123 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 10 jojo123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I did not understand...

your example:

X="c"
awk -v XYZ=$X '/c/ {print $1}' filename

i want to use variable instead of value,i mean to replace the "c"

'/c/' to variable,so i can get change it according to the input i get,
"c" or "b" ot "a".

Thanks,

Reply With Quote
  #4  
Old February 4th, 2004, 10:38 AM
druuna druuna is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 137 druuna User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 37 sec
Reputation Power: 0
Sorry, my mistake!

But even with the intention I had it would not work. It seems that you can't (or better: I don't know how to) use a variable inside the /<pattern>/ construction in awk.

I do have another sollution, which works around the problem. Bit more complicated:

X="c"
awk -F= -v XYZ=$X '$1 ~ XYZ {print $0}' filename


First the fieldseperator is set to = (-F=). You will end up with not 1 field (a=1) but with 2 ( a and 1).

Next, pass variable to awk (-v XYZ=$X)

$1 ~ XYZ This construction does the following:

Compare a certain input field (in this case $1, which is c) and compare it (~) to a value/variable (in this case XYZ, which contains the value of $X, which contains c)

If the comparison matches awk prints the entire input record {print $0}, if it doesn't it checks the next record.

Hope this is not too complicated, if you have any questions just ask them.

Reply With Quote
  #5  
Old February 4th, 2004, 12:30 PM
jojo123 jojo123 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 10 jojo123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks,

It was very easy to understand,and it works.

10X again.

Reply With Quote
  #6  
Old February 4th, 2004, 12:55 PM
jojo123 jojo123 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 10 jojo123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
One last question....

I want to get an input from the user and to search the value by the command that you wrote'if there is such a value in the file,to print "OK".

i don't know what i'm doing wrong...

#!/bin/ksh
print -n "enter value you want to search"
read X
y=`awk -F= -v XYZ=$X '$1 ~ XYZ {print $1,$2}' myfile `
if [ $y = $X ]
then
print "OK"
fi

i get the following error message:
./test[10]: [: 3: unexpected operator/operand

Thanks,

Reply With Quote
  #7  
Old February 4th, 2004, 01:47 PM
druuna druuna is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 137 druuna User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 37 sec
Reputation Power: 0
This:
if [ $y = $X ]
should be:
if [[ $y = $X ]]

There is another thing that will not work:

Your awk statement prints one field ($2) to many. Now y is filled with c 3, there will always be a number on the end. If, like I understood from your previous posts, you only want to enter a,b,c etc, you have to remove the ,$2 part.

Here's your code, a bit rewritten and working.
Code:
#!/bin/ksh

print -n "enter value you want to search "

read X

y=`awk -F= -v XYZ=$X '$1 ~ XYZ {print $1}' filename`

if [[ $y = $X ]]
then
  print "OK"
else
  print "Not OK"
fi


One more piece of advise: Make you variables human readable. Use INPUT, or USER_INPUT instead of X and User_Input instead of XYZ.

In small scripts it might not be that important, but if you start writing longer scripts/programs it's very pleasent to know what a variable is used for. It will also help others (or yourself after a long time) to figure out what the script is doing. Maybe not that important if you only script at home, but it is if others are going to see/use your scripts.

Last edited by druuna : February 4th, 2004 at 01:54 PM.

Reply With Quote
  #8  
Old February 5th, 2004, 03:09 AM
jojo123 jojo123 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 10 jojo123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Sorry again,
It's not working...

i enter "c" and in my file i have "c" and message "not ok"

the code:

#!/bin/ksh

print -n "enter value you want to search "

read X

y=`awk -F= -v XYZ=$X '$1 ~ XYZ {print $1}' myfile`
print $y
print $X

if [[ $y = $X ]]
then
print "OK"
else
print "Not OK"
fi

the message:

enter value you want to search c
c
c
Not OK

Can you please check what is the problem

Reply With Quote
  #9  
Old February 5th, 2004, 06:55 AM
druuna druuna is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 137 druuna User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 37 sec
Reputation Power: 0
If I copy/paste your example it works:

enter value you want to search c
c
c
OK

Only thing I can think of: Is your input file still the same (compared to the one in your first post). I'm using:

$ cat myfile
a=1
b=2
c=3

Reply With Quote
  #10  
Old February 17th, 2004, 03:48 AM
vabdussamad vabdussamad is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 1 vabdussamad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
AWK help

This script worked for me.

printf "Please enter the search string : "
read input_value
searched_value=`awk -F= ' $1 == X { print $1 } ' X="$input_value" logs1`
if [ "$input_value" = "$searched_value" ]
then
print "OK"
fi

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > AWK command


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway