LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 04-14-2011, 02:47 PM   #1
BarataPT
LQ Newbie
 
Registered: Mar 2011
Posts: 12

Rep: Reputation: 1
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
 
Old 04-14-2011, 02:52 PM   #2
serafean
Member
 
Registered: Mar 2006
Location: Czech Republic
Distribution: Gentoo, Chakra
Posts: 871
Blog Entries: 9

Rep: Reputation: 103Reputation: 103
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
 
Old 04-14-2011, 03:21 PM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
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.
 
Old 04-14-2011, 04:15 PM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,150

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
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
}
 
Old 04-15-2011, 07:56 AM   #5
BarataPT
LQ Newbie
 
Registered: Mar 2011
Posts: 12

Original Poster
Rep: Reputation: 1
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
 
Old 04-15-2011, 08:30 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Code:
somestuff=$(zcat session.txt.gz | wc -l)

... later ...

awk -v result=$somestuff
 
Old 04-15-2011, 08:35 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,517

Rep: Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896
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??
 
Old 04-15-2011, 09:23 AM   #8
BarataPT
LQ Newbie
 
Registered: Mar 2011
Posts: 12

Original Poster
Rep: Reputation: 1
Good point grail, didn't remembered that.

Thank you all.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pass a shell variable to an AWK command chogall Programming 1 12-23-2010 10:12 AM
awk - gensub first result webhope Programming 3 05-27-2010 05:52 AM
How do you pass an expect command result into a variable and then use it again? edomingox Programming 2 05-09-2010 01:51 AM
How to pass the result of a command to another command (like grep) desb01 Programming 4 06-25-2009 12:09 PM
How to pass a result of exec command in perl to a variable??? HyperTrey Programming 3 05-23-2008 12:47 PM


All times are GMT -5. The time now is 03:29 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration