execute shell command and store output 
Author Message
 execute shell command and store output

Hi,

How can I execute a shell command and store the output into a variable,
without it being displayed on standard out?

I tried to use system() but could not store the output. What I would
like to do is something like (sorry if the usage of system does not make
sense here, it is only for illustration)

OUTPUT=system("mycommand")

if (OUTPUT == mycode)
{      
        # modify output as I like then
        print OUTPUT

Quote:
}

Thanks,

Michael



Wed, 19 Feb 2003 09:16:02 GMT  
 execute shell command and store output

writes

Quote:
>Hi,

>How can I execute a shell command and store the output into a variable,
>without it being displayed on standard out?

>I tried to use system() but could not store the output. What I would
>like to do is something like (sorry if the usage of system does not make
>sense here, it is only for illustration)

>OUTPUT=system("mycommand")

>if (OUTPUT == mycode)
>{      
>       # modify output as I like then
>       print OUTPUT
>}

>Thanks,

>Michael

Hello Michael,

Here's an example

BEGIN {
  cmd="ls -l"
  while ((cmd | getline output)==1) {
    if (output~/cmd.awk/) {
      print output
    }
  }
  close(cmd)

Quote:
}

Instead of using system() you should pipe the output of the command into
getline. If you follow getline with a variable name the record is put
into that variable. There is no output to the screen except what you
print unless there is an error in execution of the command. Getline
returns a value of 1 if it is succesful in reading a record. You should
close the pipe when you are finished with it. You don't have to do all
this in a BEGIN block but it makes sense to do so if there is no input
other than the pipe.

Tested using gawk 3.0.6

Hope this helps
--
Alan Linton



Wed, 19 Feb 2003 22:56:34 GMT  
 execute shell command and store output

% How can I execute a shell command and store the output into a variable,
% without it being displayed on standard out?

use getline

  cmd = "mycommand"
  while ((cmd | getline OUTPUT) > 0)
    print OUTPUT # or whatever
  close(cmd)

--

Patrick TJ McPhee
East York  Canada



Wed, 19 Feb 2003 23:37:05 GMT  
 execute shell command and store output
Hi Patrick,

Thanks again for your help, I appreciate it. I have now completed the
script, apart from a slight problem. The code below prints output to one
of two files only, but it does so in a rather strange way. The file
"prodonefile" is written to correctly, but file "rejectprodfile" never
gets written to correctly. Is there something missing in the awk code,
which is obvious as to why this may be the case? I have completed my
script to process the file, which splits one file into 3 files but there
is a problem with the if/else part. An extract of the code follows

DATARECORD="D"

recordCode=substr($1, 1, 1)

if (recordCode == DATARECORD)
{
   ELEM_CODE=substr($1, 2, 12)

   CMD = "dbexec -s \"SELECT product_code FROM element WHERE elem_code =
'"'"'" ELEM_CODE "'"'"';\""

   while ((CMD | getline OUTPUT) > 0)
   {
       if (output in aProdOne)                  # aProdOne is an associative array
          print OUTPUT >> "prodonefile"
       else if (output in aProdTwo)             # aProdTwo is an associative array
          print OUTPUT >> "prodtwofile"
       else
          print OUTPUT >> "rejectprodfile"
   }
   close(CMD)

Quote:
}

Regards,

Micahel



Sat, 22 Feb 2003 18:09:24 GMT  
 execute shell command and store output


   >Thanks again for your help, I appreciate it. I have now completed
   >the script, apart from a slight problem. The code below prints
   >output to one of two files only, but it does so in a rather strange
   >way. The file "prodonefile" is written to correctly, but file
   >"rejectprodfile" never gets written to correctly. Is there
   >something missing in the awk code, which is obvious as to why this
   >may be the case? I have completed my script to process the file,
   >which splits one file into 3 files but there is a problem with the
   >if/else part. An extract of the code follows

Your script uses "output" and "OUTPUT".  Awk variable names are case sensitive.

   >DATARECORD="D"
   >recordCode=substr($1, 1, 1)
   >if (recordCode == DATARECORD)
   >{
   >ELEM_CODE=substr($1, 2, 12)
   >CMD = "dbexec -s \"SELECT product_code FROM element WHERE elem_code
   >= '"'"'" ELEM_CODE "'"'"';\""
   >while ((CMD | getline OUTPUT) > 0)
   >{
   >if (output in aProdOne)   # aProdOne is an associative array
   >print OUTPUT >> "prodonefile"
   >else if (output in aProdTwo)  # aProdTwo is an associative array
   >print OUTPUT >> "prodtwofile"
   >else
   >print OUTPUT >> "rejectprodfile"
   >}
   >close(CMD)
   >}
   >Regards,
   >Micahel

Net-Tamer V 1.08X - Test Drive



Sun, 23 Feb 2003 10:39:56 GMT  
 execute shell command and store output

% of two files only, but it does so in a rather strange way. The file
% "prodonefile" is written to correctly, but file "rejectprodfile" never
% gets written to correctly. Is there something missing in the awk code,

The code looks fine. Assuming you have data which is in neither aProdOne
nor aProdTwo, I would expect it to be written to the other file.

Are you opening a lot (say, on the order of 20) of other files? awk has
been known to impose limits on the number of concurrently open files. If
you write to a lot of files, you might try calling close on a few of them
(ie close("prodonefile")) and see if that helps.

[...]
%    while ((CMD | getline OUTPUT) > 0)
%    {
%        if (output in aProdOne)                        # aProdOne is an associative array
%           print OUTPUT >> "prodonefile"
%        else if (output in aProdTwo)           # aProdTwo is an associative array
%           print OUTPUT >> "prodtwofile"
%        else
%           print OUTPUT >> "rejectprodfile"
%    }
%    close(CMD)
--

Patrick TJ McPhee
East York  Canada



Sun, 23 Feb 2003 10:58:48 GMT  
 execute shell command and store output
I'm trying to parse a bar delimited data file using awk, I need a
middle initial from  the data element that contains a full name e.g.
|Larry R. Ainsworth|  I have a c program that will parse the middle
initial and I have tried using  "name -o %m" $8 | getline middle but
get an error message from the name command I don't think that it sees
$8 which contains the full name. I hope this makes sense.  Help!!!!


Wed, 26 Feb 2003 11:31:25 GMT  
 execute shell command and store output


Quote:
>I'm trying to parse a bar delimited data file using awk, I need a
>middle initial from  the data element that contains a full name e.g.
>|Larry R. Ainsworth|  I have a c program that will parse the middle
>initial and I have tried using  "name -o %m" $8 | getline middle but
>get an error message from the name command I don't think that it sees
>$8 which contains the full name. I hope this makes sense.  Help!!!!

Hello Larry,

"name -o %m " $8 | getline middle

should work - note the space added after %m

It seems a little strange to use an external program for something that
awk could do internally.

n=split($8,nm,"[. \t]+")
middle= (n>=3) ? substr(nm[2],1,1) : ""

I used the following program to try it out:-

BEGIN {FS="|"}

{
  n=split($8,nm,"[. \t]+")
  middle= (n>=3) ? substr(nm[2],1,1) : ""
  print "middle=",middle,"n=",n,"name=",$8

Quote:
}

I used this data file:-

1|2|3|4|5|6|7|Larry R. Ainsworth|
1|2|3|4|5|6|7|William Butler Yeats
1|2|3|4|5|6|7|e e cummings
1|2|3|4|5|6|7|J R R Tolkien
1|2|3|4|5|6|7|Julius Caesar
1|2|3|4|5|6|7|Madonna
1|2|3|4|5|6|7|the artist now known as arrow squiggle trumpet circle
1|2|3|4|5|6|7|George Hamilton IV

I got these results using gawk 3.0.6 and awk95:-

middle= R n= 3 name= Larry R. Ainsworth
middle= B n= 3 name= William Butler Yeats
middle= e n= 3 name= e e cummings
middle= R n= 4 name= J R R Tolkien
middle=  n= 2 name= Julius Caesar
middle=  n= 1 name= Madonna
middle= a n= 9 name= the artist now known as arrow squiggle trumpet
circle
middle= H n= 3 name= George Hamilton IV

I would say the results are right except for the last two but I would
like to know if the C program does better in these cases.

Hope this helps
--
Alan Linton



Wed, 26 Feb 2003 17:49:33 GMT  
 execute shell command and store output

   >I'm trying to parse a bar delimited data file using awk, I need a
   >middle initial from  the data element that contains a full name e.g.
   >|Larry R. Ainsworth|  I have a c program that will parse the middle
   >initial and I have tried using  "name -o %m" $8 | getline middle but
   >get an error message from the name command I don't think that it
   >sees $8 which contains the full name. I hope this makes sense.
   >Help!!!!
You could try breaking it down into:
cmd="name -o %m " $8
cmd | getline middle
or you could do it without the external program:
if(split($8,names," ") > 2) middle=names[2];else middle=""
The "if" allows for people like myself who do not have a middle name.
Of course some names contain words like "de", "von", etc. which are not
considered middle names...

Bill von de Marcum III ;)
"His name is Winnie _ther_ Pooh.  You know what _ther_ means, don't you?"

Net-Tamer V 1.08X - Test Drive



Thu, 27 Feb 2003 03:06:31 GMT  
 execute shell command and store output


% |Larry R. Ainsworth|  I have a c program that will parse the middle
% initial and I have tried using  "name -o %m" $8 | getline middle but

Although I probably wouln't solve this particular problem this way,
I suspect the problem is that your C program is expecting argv[1] to be
the full name, but the command line is giving the name to the C program
as 3 argments, argv[1], argv[2], and argv[3]. You could quote it:
 "name -o %m '" $8 "'" | getline middle

--

Patrick TJ McPhee
East York  Canada



Thu, 27 Feb 2003 23:46:12 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. How do I execute a shell command and capture the standard output

2. How to execute TCL command stored in a variable

3. Can I execute shell commands from awk?

4. executing Shell commands

5. Help: Executing shell commands under OS X PB.

6. executing a shell command within AWK

7. Executing shell commands

8. Regina can't execute shell commands under WIN 95/98SE

9. executing shell commands

10. Execute a shell command, how?

11. executing shell commands

12. Quick question: How to execute shell command inside a program

 

 
Powered by phpBB® Forum Software