LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Pass Command Result to Awk (http://www.linuxquestions.org/questions/programming-9/pass-command-result-to-awk-875062/)

BarataPT 04-14-2011 02:47 PM

Pass Command Result to Awk
 
Hi,

I want to pass the last command result to an awk variable, so i can use it.

I already do a similar thing with the shell loop.

Quote:

for i in {1..9}; do zcat session.txt.gz | awk -v i=$i ...
Before this i do another command, and i want to pass the result, so that i can use it in awk. How can i pass the 1st command result to awk, and use it as a var(RESULT).

Quote:

zcat session.txt.gz | wc -l
Quote:

for i in {1..9}; do zcat session.txt.gz | awk -v i=$i 'BEGIN{session="1";count=0;sum=0} {if ($1==session) count++; else {if(count == i) sum+=1;session=$1;count=1}} END{if (count == i) sum+=1; print i,"Pesquisa(s): ",sum/RESULT}'; done

serafean 04-14-2011 02:52 PM

Hi,
Are you looking for launching a subprocess and using its result? Two ways come to mind (I'll use your command) :
Code:

awk -v i=`zcat session.txt.gz | wc -l`
or
Code:

awk -v i=$(zcat session.txt.gz | wc -l)
I hope this is what you're looking for...

Serafean

MTK358 04-14-2011 03:21 PM

Note that unless you're going to be running this on other shells than bash, you should use the $() syntax. It's easier to read and nest.

PTrenholme 04-14-2011 04:15 PM

On the other hand, if you're using gawk rather than awk, you don't need the shell script at all.
Code:

# Run a command, printing any TTY output.
# Print a message to stderr and exit on failure
# Note: Only the succeed/fail code is returned. See cmd_result() for string/array returns
function run_cmd(\
        cmd,            # Command to be run\
        error_message,  # Message to print if the command fails\
        exit_code)      # If not zero, exit code to use if the command fails. (0 for "do not exit")
{
  if (debug) printf("Debug: run_cmd(\"%s\") ", cmd)
  if (system(cmd)) {
    if (debug) print cmd ": failed."
    if (error_message != "") print cmd ": " error_message > "/dev/stderr"
    if (exit_code != 0) exit(exit_code)
    return 1
  }
  if (debug) print  " succeed."
  return 0
}
# Run a command and return its output. If more than one result is returned by the system,
# only the first result will be returned. If all results are desired, pass the optional array
# as the fourth argument.
function cmd_result(\
  cmd,          # Command to execute\
# Optional arguments\
  error_message,# Error message if the expected result does not return\
  expect,      # Expected result (one of the words: fail, string{s}, number{s}, or list) ("string" assumed is omitted.\
  result,      # If provided, the array of results.\
# Local variables\
  i,\
  temp,\
  n,\
  ok)
{
# Sanity checks
  if (cmd=="") {
    ok=1
    print "cmd_result() was called with a null command." > "/dev/stderr"
  }
  if (expect == "") {
    expect="string"
  }
  else {
    expect=tolower(expect)
  }
  if (!((expect ~= /^number[s]?$/) || (expect ~= /^string[s]?/)") || (expect == "fail") || (expect == "list"))) {
    ok=1
    print "cmd_result(" cmd", "\" error_message "\", \"" expect ": Must be \"fail\", \"number[s]\", \"string[s]\", or \"list\"" >"/dev/stderr"
  }
  if (ok) return 2
#
  n = 0
  if (debug) printf("Debug: cmd_result(\"%s\") =", cmd)
  result[1]=""
  while (cmd | getline temp) {
    if (debug) printf("\t\"%s\"\n", temp)
    ++n
    result[n] = temp
  }
  close(cmd)
  do {
    if ( expect == "fail" ) {
      if ( result[1] ) {
        if (error_message != "") {
          print cmd ": " error_message > "/dev/stderr"
        }
        else {
          print cmd  did not fail." > "/dev/stderr"
        }
      }
      break
    }
    if ((n > 1) && (expect !="list") && (expect !~ /s$/)) {
      print cmd ": Warning: " n " values were returned, not one."
      break
    }
    if (expect ~ /^number/) {
      for (i=1; i<=n;++i) {
        result[i] = 0 + result[i]
      }
      break
    }
    else {
      break
    }
  } while (1)
  temp=result[1]
  return temp
}


BarataPT 04-15-2011 07:56 AM

Thanks. Worked fine.

Just one more thing. Is there some way i can do that out of the cycle, so that the subprocess is only executed one time:

Quote:

for i in {1..9}; do zcat session.txt.gz | awk -v i=$i -v result=$(zcat session.txt.gz | wc -l) 'BEGIN{session="1";count=0;sum=0} {if ($1==session) count++; else {if(count == i) sum+=1;session=$1;count=1}} END{if (count == i) sum+=1; print i,"Pesquisa(s): ",sum/result}'; done

MTK358 04-15-2011 08:30 AM

Code:

somestuff=$(zcat session.txt.gz | wc -l)

... later ...

awk -v result=$somestuff


grail 04-15-2011 08:35 AM

Well seeing it is not reliant on the index in the for loop, yes just run it prior to the loop and set it the same way once inside.
I must say I am not sure why you bother though as you could just do it all in awk. Also, You do realise that once you have read the whole file, ie once in END part of awk, that
your result value you have retrieved earlier is just the NR value??

BarataPT 04-15-2011 09:23 AM

Good point grail, didn't remembered that.

Thank you all.


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