LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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-17-2002, 08:14 AM   #1
xanthium
Member
 
Registered: Apr 2001
Location: Bombay ( INDIA )
Distribution: RedHat 7.x
Posts: 218

Rep: Reputation: 30
Question Awk and Shell CMD Output


Hi ,

Iam rather new to awk. Yeh i know awk is used for processing upon rows of strings and have written two bit scripts. My question is :

is it possible to execute a shell command from within the awk script and get its O/P(which will be multiline) for the awk script to interpret. But the catch is that i dont want to write any shell script ( only one single awk script). Right now iam using the "system" command available with nawk but it only returns the exit status of the concerned shell command and not the actual o/p!

All help will be appreciated.

Regards,
Xanthium

 
Old 04-17-2002, 03:43 PM   #2
noodle123
LQ Newbie
 
Registered: Apr 2002
Posts: 27

Rep: Reputation: 15
Yes it is possible to execute an external script by writing an awk script, but is very complicated. I would suggest another approach. Perhaps a 2 process approach. Multiline awk scripts can get very confusing.
 
Old 04-18-2002, 12:13 AM   #3
xanthium
Member
 
Registered: Apr 2001
Location: Bombay ( INDIA )
Distribution: RedHat 7.x
Posts: 218

Original Poster
Rep: Reputation: 30
Smile thanks

Hi Noodle123,

i guess the getline function does the trick but since i dont know how to go about it ( tried it but got error message ) i have now left the idea but still am curious if its possible (and a real example de monstrated could help ... ha ha ha )

thanks for your reply and efforts.

regards,
xanthium.

 
Old 04-18-2002, 09:23 AM   #4
noodle123
LQ Newbie
 
Registered: Apr 2002
Posts: 27

Rep: Reputation: 15
Hey,

The getline cmd will probably work, but i still don't really understand what u're trying to use the awkscript to do... perhaps give an example? I've used getline before, and i believe if used in conjunction with an array it would make the script flow a lot better.

Noodles.
 
Old 04-19-2002, 01:40 AM   #5
xanthium
Member
 
Registered: Apr 2001
Location: Bombay ( INDIA )
Distribution: RedHat 7.x
Posts: 218

Original Poster
Rep: Reputation: 30
Lightbulb for parsing netstat result

HI Noodle ,

I want to write a self contained awk script ( no shell script !)
which shall execute the netstat command and interpret its result
... now i am comfortable with teeny weeny awk usages so can
quite comfortably parse the results but dont know how to make awk script to run the shell command and then interpret the result although i can do so with the combination of shell script and awk script but am unable to do so with a single AWK script (ie;without using a shell script as well)

I hope i made it clear for you.

Thanks for your reply.

Best Regards,
Xanthium.
 
Old 04-19-2002, 04:22 AM   #6
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
The gnu awk manual is a very helpful thing if you want to write awk scripts:

http://www.gnu.org/manual/gawk-3.0.3/gawk.html

Anyways I put a simple script together so all it does is just print the output of each line. But you said you where familiar with simple awk stuff so I'm sure you can do the rest. If you run a 'chmod +x scriptname' then this script can be run just like you run any other normal script.

Code:
#!/bin/awk -f

BEGIN {
  command="netstat"
  while ((command | getline) > 0)
    print 
}
 
Old 04-19-2002, 05:42 AM   #7
xanthium
Member
 
Registered: Apr 2001
Location: Bombay ( INDIA )
Distribution: RedHat 7.x
Posts: 218

Original Poster
Rep: Reputation: 30
thanks

Hi ,

Thanks for ur suggestion.
Yeh it did work for me NOW.


Thanks a lot.

Regards,
Xanthium.
 
Old 04-20-2002, 12:06 PM   #8
xanthium
Member
 
Registered: Apr 2001
Location: Bombay ( INDIA )
Distribution: RedHat 7.x
Posts: 218

Original Poster
Rep: Reputation: 30
Unhappy bothering u again

Hi ,

Here iam back with my original problem.

Yeh ur suggestion did help me a lot but am lost again ( Due to my poor knowledge of AWK )

Well my script basically runs the netstat command and loops through the network interfaces available( except the LO interface)

Here is how my script looked ( a section ie

command_interface="netstat -i | awk '/en/ && $1 !=prev {print $1; prev=$1}'"

#Looping through the Interfaces
while ((command_interface | getline) > 0)

Now i take the o/p from the above mentioned command ( the o/p will be en0, en1, en2, etc etc) and give it to another long command. But the problem is iam unable to concatenate strings!!

so i cant do following
command1="my-command $0 -d"

Above i had assumed that in the above variable ie; command1,$0 will be substituted with the o/p of the first while-getline combo.

I hope u got my point ! in other words :

mystring=some-string-without-double-inverted-comma


how do i merge the content of above mentioned variable in
some other string ?


Can u help me out ?


Regards,
Xanthium.

Last edited by xanthium; 04-20-2002 at 02:10 PM.
 
Old 04-22-2002, 05:06 AM   #9
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
See the other thread but basically you can just add strings after each other.

command1=mycommand " " $0 " -d"

I guess you mean something like that. I don't know what you mean with the inverted comma.
 
Old 04-22-2002, 06:21 AM   #10
xanthium
Member
 
Registered: Apr 2001
Location: Bombay ( INDIA )
Distribution: RedHat 7.x
Posts: 218

Original Poster
Rep: Reputation: 30
Arrow for ex; "Xanthium" and Xanthium !

Hi ,


example :

var1="I am an enclosed in inverted commas"
var2=plain string
var3="enclosed again"

now i want a variable called var4 with value equal to

I am an enclosed in inverted commas plain string


so to do that i wrote following asssignment in my script
var4=var1" "var2


but well mik ... awk expects var2="plain string" for the above assignment to work !!! and not [ var2=plain string ] as i have written ....


now do u get my point ?

regards,
xanthium.
 
Old 04-22-2002, 07:55 AM   #11
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
Oh that's what you mean with inverted comma's. I usually call them double quotes.

I get your point but unfortunately you can't do anything about that. That's the way awk works. If you want to quote a literal string then you have to enclose it in double quotes. Otherwise it sees the words as variables.

Doing the assignment:
var2=plain string

would imply that var2 should get the value of plain and the value of string. Now plain and string are obviously non existing variables so the var2 will just be an empty string.
I don't see why it's a limitation to put double quotes around the string though. It's normal to do that in almost any scripting/programming language.
 
Old 04-23-2002, 12:54 AM   #12
xanthium
Member
 
Registered: Apr 2001
Location: Bombay ( INDIA )
Distribution: RedHat 7.x
Posts: 218

Original Poster
Rep: Reputation: 30
Thanks

Hi Mik ,

Thanks a lot for your I/P... i got your point. Please excuse my english ( regarding double quotes) speaking skills.

I will tell you about my awk script.

Basically my script gets the name of all network interfaces available on the machine ( stores the names in a variable) .... now obviously it WONT be stored with double quotes.

Now i run the "entstat" command with the name of the interface as one of the parameter and pass it to getline function ( This again is something i learned from you ... thanks) but since the interface name is in a variable i can't form a string with the name and pass it to getline !!!

ex:

var1=ent1 <<< Stored dynamically !!!!

mycommand="entstat -d var1" <<<<< HERE i WANT val of var1
to be substituted by ent1.
while ((mycommand | getline) > 0)


I hope u got my point ?

Now iam feeling that maybe AWK is not the right tool for this job .We have similar shell script doing the same thing but it is so heavily embedded with AWK commands that we felt it would help if the whole thing was written in AWK itself!

Thanks for your help and patience.

Regards,
Xanthium.
 
Old 04-23-2002, 04:28 AM   #13
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
Ok from what I understand you want to first read all the interfaces using the command 'netstat -i'. And then for each interface you want to run some other command. Would that be something like this?

Code:
#!/bin/awk -f

BEGIN {
  command="netstat -i"
  line=0
  while ((command | getline) > 0)
  {
    line+=1

    # first two lines aren't interfaces
    if ( line > 2 )
    {
      interface=$1

      print "Getting IP address for interface " interface
      int_command="/sbin/ifconfig " interface
      while ((int_command | getline) > 0)
      {
        if ($1 == "inet")
          print $2
      }
    }
  }
}
This simple example just gets the ip address of the interface using ifconfig.
 
Old 04-23-2002, 09:05 AM   #14
xanthium
Member
 
Registered: Apr 2001
Location: Bombay ( INDIA )
Distribution: RedHat 7.x
Posts: 218

Original Poster
Rep: Reputation: 30
thanks .. will try tomorrow

Hi ,

Thanks a lot ..... i read ur post just as i was leaving for the day.
I Will write the code tomorrow and let u know ( i have a inkling it will work). I faced one more problem today ( u must be wondering "OH NO ... HERE HE GOES AGAIN !") ...

I did something like this :

BEGIN {
printf("lsps -a | awk ' NR >1 {print $5}' >> report") | "/bin/sh"
printf("lsps -a | awk ' NR >1 {print $5}' >> report") | "/bin/sh"
}

It works if only one printf statement is given but wont work if stated as above.... my guess it has something to do with close()
but couldnt figure out what to close ????

BTW : Iam using AIX ( not Linux).

anyway THANKS A LOT .... i MEAN it.

Regards,
Xanthium.
 
Old 04-24-2002, 03:53 AM   #15
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
You should be using print instead of printf for that because you haven't specified any format fields.

print [ expression_list ] [ >file | >>file ] [ | command ]
printf format[ ,expression_list ] [ >file | >>file ] [ | command ]
printf format [ ,expression_list ] [ >file ]

It doesn't really matter that much which platform you are creating it on. I created some of those test scripts on Digital unix and they work just as well on linux.
 
  


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
Accessing Shell variable in awk script dileepkk Linux - General 1 10-07-2004 07:47 AM
Awk output Trouble fooforon Programming 6 04-12-2004 07:14 AM
xcdroast - cdrecord.mmap: Input/output error. write_g1: scsi sendcmd: cmd timeout masgari Linux - Software 11 03-17-2004 10:55 PM
store output of cmd in array? h/w Programming 6 10-09-2003 08:46 PM
How do I zip and attach the output data of a grep | awk | mail shell script? 360 Programming 1 05-08-2002 08:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:42 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration