Awk: How to pass/store a command output in variable?
Hello,
How can we pass/store the output of a command in awk? For example, consider:- Code:
awk -v val=$(some_command) '{if($1==$val) print $0' infile.txt Thanks in advance. |
awk doesn't use $ to refer to variables:
Code:
awk -v val=$(some_command) '{if($1==val) print $0}' infile.txt |
Code:
MYVAR='load average: ' && uptime | awk -F "$MYVAR" '{ print $2 }' 0.47, 0.46, 0.43 This is how I have used variables with awk in the past Again, it is only an example, and a working one at that. ;) Edit: ntubski: I am not trying to be contrary here. But did you mean $(command_substitution)? |
Quote:
|
ntubski:
Thanks! |
If you are actually using gawk instead of mawk (look at ls -l /bin/awk to see where your awk command is symlinked), then the process is quite easy. Here's a gawk program I keep in /usr/share/awk/:
Code:
############################################################################################## |
Thanks for your responses.
I was actually trying, first command either:- Code:
awk '/sample/ {print NR}' infile Code:
awk -v var=$(day +%d) '{if(NR>=$var) print $0}' infile Code:
awk -v day=(date +%d) '{if(NR==$day) print $0}' infile |
Quote:
Code:
awk -v day=$(date +%d) '{if(NR==$day) print $0}' infile |
@ntubski: You are right. It worked fine. Many thnaks!! :)
|
All times are GMT -5. The time now is 05:59 AM. |