LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Awk: How to pass/store a command output in variable? (https://www.linuxquestions.org/questions/linux-newbie-8/awk-how-to-pass-store-a-command-output-in-variable-4175444183/)

shivaa 01-04-2013 01:26 PM

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
But such method isn't giving desired output. Could you suggest how to do this?
Thanks in advance.

ntubski 01-04-2013 03:59 PM

awk doesn't use $ to refer to variables:
Code:

awk -v val=$(some_command) '{if($1==val) print $0}' infile.txt
#                                  ^
#                                  no $ here


Habitual 01-04-2013 05:48 PM

Code:

MYVAR='load average: ' && uptime | awk -F "$MYVAR" '{ print $2 }'
spits out
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)?

ntubski 01-04-2013 06:10 PM

Quote:

Originally Posted by Habitual (Post 4863191)
I am not trying to be contrary here. But did you mean $(command_substitution)?

No, shivaa was using $val inside the awk program, but really just plain val was meant. In awk, $val means the valth field in the current record.

Habitual 01-04-2013 06:56 PM

ntubski:

Thanks!

PTrenholme 01-04-2013 06:59 PM

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:

##############################################################################################
#
# Run a command and return its output. If more than one result is returned by the system,
# the return value will be the first value returned by the command.
#
# If more than one value is desired, pass the optional array name as the second argument
# and set the third argument to "list."
#
#############################################################################################
function cmd_result(\
  cmd,          # Command to execute\
# Optional arguments\
  results,      # Array in which the results should be returned\
  expect,      # Expected result (either "fail" or "list") Default: Single returned value\
  error_message,# Error message\
# Local variables\
  result_string,\
  temp,\
  n)
{
  n = 0
  result_string=""
  while (cmd | getline temp) {
    d_print("\"" temp "\"","b")
    ++n
    result_string = ((result_string) ? result_string SUBSEP : "") temp
  }
  close(cmd)
  if ( expect == "fail" && n > 0) {
    if (error_message) {
      print "cmd_result(): " error_message > "/dev/stderr"
    }                 
    else {
      print "cmd_result(): " cmd " did not fail." > "/dev/stderr"
    }
  }
  if ((n > 1) && (expect !="list")) {
    print "Warning (cmd_result()): " cmd " returned " n " values."
  }
  split(result_string, results, SUBSEP)
  return results[1]
}


shivaa 01-04-2013 10:14 PM

Thanks for your responses.
I was actually trying, first command either:-
Code:

awk '/sample/ {print NR}' infile
OR
date +%d

And then:
Code:

awk -v var=$(day +%d) '{if(NR>=$var) print $0}' infile
Or even if I try without $, like:
Code:

awk -v day=(date +%d) '{if(NR==$day) print $0}' infile
But none of these working. Problem is that how to store output of first command in var to make a one-line second command? I know bash style of variable decalration won't work here, so is there any alternative?

ntubski 01-05-2013 10:22 AM

Quote:

Originally Posted by shivaa (Post 4863265)
Or even if I try without $, like:
Code:

awk -v day=(date +%d) '{if(NR==$day) print $0}' infile

You removed the wrong $. The green $ is interpreted by bash, it is necessary. The red $ is inside single quotes so it is not interpreted by bash, but passed to awk. As I mentioned in post #4, awk interprets $day as the dayth field.
Code:

awk -v day=$(date +%d) '{if(NR==$day) print $0}' infile
awk -v day="$(date +%d)" '{if(NR==day) print $0}' infile # remove red $ to make it correct
# also, if the command output has spaces, double quotes are required.
# In this case output has no spaces but it doesn't hurt to quote.


shivaa 01-05-2013 12:30 PM

@ntubski: You are right. It worked fine. Many thnaks!! :)


All times are GMT -5. The time now is 05:59 AM.