|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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, |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
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, |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
Thanks,
It was very easy to understand,and it works. 10X again. |
|
#6
|
|||
|
|||
|
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, |
|
#7
|
|||
|
|||
|
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. |
|
#8
|
|||
|
|||
|
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 |
|
#9
|
|||
|
|||
|
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 |
|
#10
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > AWK command |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|