If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > unix command using in awk

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-21-04, 11:35
fla5do fla5do is offline
Registered User
 
Join Date: Oct 2003
Location: Germany
Posts: 138
unix command using in awk

Hi,
I want to execute a system call in awk and store the result in a awk variable. I get the result from the command, but not the output which is given from it.

for example :

awk ......
x=system("date")

I need a solution in awk !!! and not by using shell in this form :

DATE=ŽdateŽ
awk -v DATE=$DATE .....

Thanx for your interest.
__________________
Greetings from germany
Peter F.
Reply With Quote
  #2 (permalink)  
Old 02-21-04, 12:42
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
You can execute a command and read the result with 'getline' using the syntax :
Code:
command | getline [variable]
The following awk script read commands, execute them and print result.

Code:
awk '
function ExecCmde(cmde    ,line, result) {
   while ((cmde | getline line) > 0) {
      result = result (result=="" ? "" : "\n") line ;
   }
   return result;
}
tolower($1) ~ /exit|quit/ {
   exit;
}
NF>0 {
   print "===",$0,"===";
   print ExecCmde($0);
   print "===";
}
'
__________________
Jean-Pierre.
Reply With Quote
  #3 (permalink)  
Old 02-22-04, 04:23
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
a little correction, add this line in the function before 'return'
Code:
close(cmde);
__________________
Jean-Pierre.
Reply With Quote
  #4 (permalink)  
Old 02-23-04, 16:40
fla5do fla5do is offline
Registered User
 
Join Date: Oct 2003
Location: Germany
Posts: 138
Thanx aigles,
it is a very nice solution, and it works very fine when I type my "date" command followed by CR . Exit or quit ends the script. (wonderfull).
It is realy the way I want to do.
But I dont understand the source code . Is there an easier way, without typing the command. For example the "date" command as a fix text ???

Thanx a lot
__________________
Greetings from germany
Peter F.
Reply With Quote
  #5 (permalink)  
Old 02-23-04, 16:57
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
For a command that return a signgle line (like 'date') command, you can do :

Code:
  "date" | getline the_date;
  close("date");  
print the_date;

# The same, with command in a variable
  date_cmde = "date +'%d.%m.%Y'";
  date_cmde | getline the_date;
  close(date);
  print the_date;
If your command return multiple line, you get them within a loop (like in my previous post) :

Code:
   while ((cmde | getline line) > 0) {
      print line;
   }
   close(cmde);
__________________
Jean-Pierre.
Reply With Quote
  #6 (permalink)  
Old 02-24-04, 04:48
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Peter,

You can also use the 'system' subroutine to explicitly call native utilities...

awk -v cmd=date 'BEGIN{system(cmd)}'
Reply With Quote
  #7 (permalink)  
Old 02-24-04, 13:49
fla5do fla5do is offline
Registered User
 
Join Date: Oct 2003
Location: Germany
Posts: 138
Thanx Jean-Pierre,

it works well . Thank you for your interest to my problem.


Thanks Damian too, I have learn a lot with your comment.

but, when I try this :
awk -v cmd="date +%d%m%Y" 'BEGIN{
system(cmd)
x=system(cmd)
print x
}'

the printout from variable x, there is a second line with "0"

look at the output which is given :
24022004
24022004
0
__________________
Greetings from germany
Peter F.
Reply With Quote
  #8 (permalink)  
Old 02-24-04, 15:12
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
when you use 'system' function to execute an external command, you don't get the output of the command. What you get is the status.

x=system(date)
execute the command, output goes to stdout, and x is set to status of command (0 for succes);

print x
print the status of the execution of the 'date' command.

if you want to retrieve the output of the command, you must use the 'command | getline' method.
__________________
Jean-Pierre.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On