Awk: How to get an awk variable out to the shell, using system() ? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Awk: How to get an awk variable out to the shell, using system() ?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-06-2012
DerekAsirvadem DerekAsirvadem is offline
Banned
 
Join Date: Dec 2010
Last Activity: 6 December 2012, 5:45 PM EST
Location: /opt/sybase/dataserver
Posts: 12
Thanks: 6
Thanked 0 Times in 0 Posts
Awk: How to get an awk variable out to the shell, using system() ?

I am reasonably capable with awk and its quirks, but not with shell weirdness. This has to be Bourne Shell for portability reasons. I have an awk program that is working just fine; it handles multiple input streams and produces several reports, based on the request (-v Variables). In addition to processing the stream, and producing the required report in the stream (so that it can be re-directed, etc), I write certain errors to a separate file.

Now I need to enhance the program, to write to a specific filename (constructed per execution, no problem), and to sort the contents.

I used the Site Search but nothing similar came up, due to the hundreds of badly formed thread titles.

The generic question may be as simple as the thread title.

This code snippet contains the seed of the problem, in simple form. The last two lines of code is a statement of intent; obviously it does not work as is.

Code:
BEGIN {
    ## constructed, simple form provided
    ERR_FILE =          "SERVER_yymmdd_Err.dat"
    ERR_FILE_TMP = "/tmp/SERVER_yymmdd_Err.dat"
    }

/Pattern/ {
    ## many of these; works fine
    if ( Foo != Bar ) printf Value_DMP, Foo, Bar > ERR_FILE_TMP
    }

END {
    printf "SERVER\t%s %s\n", SERVER, DATE > ERR_FILE
    ## need to get the filename out to the shell
    ## this states intention:
    system( "sort ERR_FILE_TMP >> ERR_FILE" )
    system( "rm   ERR_FILE_TMP" )
    }

Alternate approaches are welcome, as long as it does not result in a major code change.
Sponsored Links
    #2  
Old 12-06-2012
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 2 September 2014, 7:34 AM EDT
Location: Amsterdam
Posts: 9,386
Thanks: 273
Thanked 2,349 Times in 2,108 Posts
You need to place the variable outside the double quotes, for example try:

Code:
system( "sort " ERR_FILE_TMP " >> " ERR_FILE )

Sponsored Links
    #3  
Old 12-06-2012
DerekAsirvadem DerekAsirvadem is offline
Banned
 
Join Date: Dec 2010
Last Activity: 6 December 2012, 5:45 PM EST
Location: /opt/sybase/dataserver
Posts: 12
Thanks: 6
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Scrutinizer View Post
You need to place the variable outside the double quotes
That was so simple ! Thanks.

But I do not see how that is "outside the double quotes". For the benefit of the understanding of others, that is "concatenate the string before submitting it to system()". And my concern about informing the shell re awk vars, is a non-issue.
    #4  
Old 12-06-2012
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 2 September 2014, 7:34 AM EDT
Location: Amsterdam
Posts: 9,386
Thanks: 273
Thanked 2,349 Times in 2,108 Posts
You're welcome. What I meant was, in your original code you had ERR_FILE_TMP and FILE_TMP inside the double quotes, so they would be interpreted by awk as a literal string instead of a variable. So yes, the way to procede then is to create the string to be passed to system() by concatenation of string parts and variables...
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to use shell variable in awk? thibodc Shell Programming and Scripting 8 04-11-2012 02:23 AM
assign awk's variable to shell script's variable? tiger2000 Shell Programming and Scripting 4 04-13-2010 03:53 PM
shell function, system, awk jkl_jkl Shell Programming and Scripting 5 06-02-2008 08:46 AM
shell variable in awk cskumar Shell Programming and Scripting 12 08-20-2007 08:41 AM
can I pass awk variable to system command? zhynxn Shell Programming and Scripting 2 07-05-2006 11:11 AM



All times are GMT -4. The time now is 07:42 AM.