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. |
Hint #1:
Code:
i=abc Code:
echo "ghi jkl" |awk '{print $2}' PS: Another good link, besides those already suggested to you: http://tldp.org/LDP/Bash-Beginners-G...ect_03_04.html |
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" For multiple variables you can have awk generate 'shell commands' and "eval" the output results. THIS IS DANGERIOUS security wise. Code:
d="string" Code:
d="string" 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". |
A.Thyssen -
1. Abid Malik's question was *entirely* appropriate for this forum: Quote:
Abid Malik - Please do post back with what you find (marking the post "SOLVED")! That way, your question (and your solution) can help others. |
@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
|
Quote:
Code:
awk -vd="$d" '$1==d{ ... }' file |
Quote:
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. |