LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   set value with awk (https://www.linuxquestions.org/questions/programming-9/set-value-with-awk-838582/)

Abid Malik 10-16-2010 11:03 PM

set value with awk
 
hello!

i want to change the value of a variable if a condition met.

i=0
awk '{if( $1 == $d ) i=2 }' file > temp
echo i = $i



but in the above code i is always equal to 0 whether the conditions are met.

paulsm4 10-17-2010 12:44 AM

Hint #1:

Code:

i=abc
echo $i

i=$(echo def)
echo $i

Hint #2:
Code:

echo "ghi jkl" |awk '{print $2}'
And by all means, please post back when you arrive at a solution, marking your post "SOLVED". You've asked a number of questions recently, but haven't responded back to anybody who's tried to help you.

PS:
Another good link, besides those already suggested to you:

http://tldp.org/LDP/Bash-Beginners-G...ect_03_04.html

A.Thyssen 10-17-2010 12:51 AM

awk is a completely seperate command and language to shells. the variables do NOT match up.

You can feed in shell variables into awk by using double quotes, but that only sets the variable contents as a constant value, not as a varable into awk.

For output you need to some how get that result back into a shell variable. typically you get awk to print it and then read the output into a shell variable.

Code:

d="string"
i=`awk '{if( $1 == "'"$d"'" ) i=2 }  END {print i}' file`
echo "$i"

If you are outputting a file and want the variable, then you need to start getting even trickier, and use awk and shell file destriptors and or temporary files.

For multiple variables you can have awk generate 'shell commands' and "eval" the output results. THIS IS DANGERIOUS security wise.

Code:

d="string"
eval `awk '{if( $1 == "'"$d"'" ) a=2; b=3 }  END {print "a=" a "; b=" b }' file`
echo "a=$a  b=$b"

Another less dangeriosu method is to use "set" on the awk output to place the output 'words' into the shell variable $1 $2 etc..

Code:

d="string"
set -- `awk '{if( $1 == "'"$d"'" ) a=2; b=3 }  END {print a, b }' file`
echo "a=$1  b=$2"

All these tricky methods are listed (somewhere) in teh Advanced BASH Shell Scripting Guide
http://www.tldp.org/LDP/abs/html/index.html

PS: I have been shell programming for more than 30 years now, and have developed and used such methods continuiously over than time. No just for awk, but sed, perl, and many other programs. The methods are pretty much the same no matter what command you use.

NOTE this topic is not really appropriate for non-*nix forum. But then as there is not specific 'shell programming' forum, where it should go is a problem. Best place seems to be "linux-general".

paulsm4 10-17-2010 01:35 AM

A.Thyssen -

1. Abid Malik's question was *entirely* appropriate for this forum:
Quote:

Programming
This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
2. The modern, bash equivalent of the backquote ("eval `CMD`") you mentioned is now the "$(CMD)" syntax I cited above.

Abid Malik -

Please do post back with what you find (marking the post "SOLVED")! That way, your question (and your solution) can help others.

grail 10-17-2010 02:55 AM

@OP - depending on what you are trying to do, obviously very hard to tell from your snippet, you may be better off just doing the whole script in awk ... just a thought

ghostdog74 10-17-2010 03:13 AM

Quote:

Originally Posted by A.Thyssen (Post 4130077)
You can feed in shell variables into awk by using double quotes,
....
PS: I have been shell programming for more than 30 years now ..

in your 30 years of shell programming, did you happen to know awk's -v option ? :)
Code:

awk -vd="$d" '$1==d{ ... }' file
this is a cleaner way to pass shell variable to awk without messy quoting.

A.Thyssen 10-17-2010 06:07 AM

Quote:

Originally Posted by ghostdog74 (Post 4130140)
in your 30 years of shell programming, did you happen to know awk's -v option ? :)
Code:

awk -vd="$d" '$1==d{ ... }' file
this is a cleaner way to pass shell variable to awk without messy quoting.

Yes and I have used it, but I also find it awk-ward (sorry could not resist the pun). Most often you don't need the shell value in an actual variable, as it s only in one place, so I use that quoting method shown to put it there (at least for a string). Other times the shell variable contains optional code, as a sort of macro expansion. And finally it does not work for other commands like sed, and perl.

As for backquotes, yes I do know that $(...) is the new way to handle command substitutions, but generally I write my shell scripts for multiple computers, including some very old manchine that either use a very old version of bash, but more likely original bourne sh (SunOS for example). I have even needed shell script than had to run without change, on ultrix. ASIDE: the default shell does not even handle functions properly!

Even Mac's do not run a very modern version of bash, and don't use many GNU versions of awk, sed, etc. I have had to re-write many scripts to make them MacOSX-compatible as well as linux compatible.

As such I usually write scripts to be as compatible as possible, and only go to bash-isms, or other less compatible feature, only I really need a bash specific feature, such as nested command substitutions (which is why $(..) was invented), or variable arrays. At that time I change the 'bang' line from "sh" to "bash" to mark the script as bash specific. Not enough people do that!


All times are GMT -5. The time now is 01:02 PM.