LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 01-04-2013, 01:26 PM   #1
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
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.
 
Old 01-04-2013, 03:59 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,455

Rep: Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843
awk doesn't use $ to refer to variables:
Code:
awk -v val=$(some_command) '{if($1==val) print $0}' infile.txt
#                                   ^
#                                   no $ here
 
Old 01-04-2013, 05:48 PM   #3
Habitual
Senior Member
 
Registered: Jan 2011
Distribution: Undecided
Posts: 3,335
Blog Entries: 5

Rep: Reputation: 785Reputation: 785Reputation: 785Reputation: 785Reputation: 785Reputation: 785Reputation: 785
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)?

Last edited by Habitual; 01-04-2013 at 05:51 PM.
 
Old 01-04-2013, 06:10 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,455

Rep: Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843
Quote:
Originally Posted by Habitual View Post
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.
 
1 members found this post helpful.
Old 01-04-2013, 06:56 PM   #5
Habitual
Senior Member
 
Registered: Jan 2011
Distribution: Undecided
Posts: 3,335
Blog Entries: 5

Rep: Reputation: 785Reputation: 785Reputation: 785Reputation: 785Reputation: 785Reputation: 785Reputation: 785
ntubski:

Thanks!
 
Old 01-04-2013, 06:59 PM   #6
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,150

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
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]
}

Last edited by PTrenholme; 01-04-2013 at 07:05 PM.
 
Old 01-04-2013, 10:14 PM   #7
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
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?

Last edited by shivaa; 01-04-2013 at 10:29 PM. Reason: Example added
 
Old 01-05-2013, 10:22 AM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,455

Rep: Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843
Quote:
Originally Posted by shivaa View Post
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.

Last edited by ntubski; 01-05-2013 at 10:23 AM. Reason: missed word
 
1 members found this post helpful.
Old 01-05-2013, 12:30 PM   #9
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
@ntubski: You are right. It worked fine. Many thnaks!!
 
  


Reply

Tags
awk


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
[SOLVED] Bash; awk or sed output to variable: how keep newline at end of each output line porphyry5 Programming 3 06-10-2011 05:50 PM
Pass a shell variable to an AWK command chogall Programming 1 12-23-2010 10:12 AM
How can I receive command output and store it to a variable athlonshi Programming 5 04-04-2010 08:09 AM
i need to store the output of bitset() in a variable anurupr Linux - Newbie 1 03-05-2010 12:32 AM
how to pass an external variable to awk??? akitty Programming 7 11-13-2008 08:47 AM


All times are GMT -5. The time now is 01:26 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