UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 15th, 2005, 10:19 PM
smokingguns smokingguns is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 16 smokingguns User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 35 m 4 sec
Reputation Power: 0
Question help with awk script needed!!!!

hey guys,
The foll. awk script(stored in filename.awk) is not working properly. In line 2
i store the output of the 'ps-fade' command in a file called op...
then i use a pattern to get the PPID( parent PID) and store it in variable 'var' using getline... Till line 3 everything is okay. Line 3 prints the value of the PPID correctly.

But when i use 'var' in the line 4 , the output of "top" isnt directed into the file "opt" . It's as if 'var' cant be accessed in line 4.. Only the value of var is printed(line 3) in "opt" file..
By the way the command inside the system function works fine on the command line,but not when i use the the command
"awk -f filename.awk" to get the behavior i want
the code --->
1 BEGIN {
2 "ps -fade >op; awk '/some pattern/ {print $3}' op " | getline var
3 print var,"\n"
4 system("top -b -d 1 $(awk '$3 == var {print \" -p\" $2}')" ) > "opt"

}

by the way in line 4 im trying to get only those processes printed in "opt" whose PPID is given by 'var'

Reply With Quote
  #2  
Old July 16th, 2005, 02:42 PM
christo's Avatar
christo christo is offline
Introspective
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Nov 2001
Location: London, UK
Posts: 3,297 christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 1 h 9 m 17 sec
Reputation Power: 104
Send a message via ICQ to christo Send a message via Yahoo to christo
Sorry, but it looks like you're trying to cut down trees with a breadknife. You really should consider using Perl for this stuff.


christo

Reply With Quote
  #3  
Old July 18th, 2005, 05:08 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,089 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 Days 3 h 39 m 54 sec
Reputation Power: 9
Quote:
Originally Posted by smokingguns
hey guys,
The foll. awk script(stored in filename.awk) is not working properly. In line 2
i store the output of the 'ps-fade' command in a file called op...
then i use a pattern to get the PPID( parent PID) and store it in variable 'var' using getline... Till line 3 everything is okay. Line 3 prints the value of the PPID correctly.

But when i use 'var' in the line 4 , the output of "top" isnt directed into the file "opt" . It's as if 'var' cant be accessed in line 4.. Only the value of var is printed(line 3) in "opt" file..
By the way the command inside the system function works fine on the command line,but not when i use the the command
"awk -f filename.awk" to get the behavior i want
the code --->
1 BEGIN {
2 "ps -fade >op; awk '/some pattern/ {print $3}' op " | getline var
3 print var,"\n"
4 system("top -b -d 1 $(awk '$3 == var {print \" -p\" $2}')" ) > "opt"

}

by the way in line 4 im trying to get only those processes printed in "opt" whose PPID is given by 'var'


post the output of your ps cmd
your OS ?
__________________
working on Solaris[5-9], preferred languages french and C.

Reply With Quote
  #4  
Old July 21st, 2005, 08:21 AM
zlutovsky zlutovsky is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Prague, Czech Rep.
Posts: 117 zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 42 m 44 sec
Reputation Power: 6
Quote:
Originally Posted by christo
Sorry, but it looks like you're trying to cut down trees with a breadknife. You really should consider using Perl for this stuff.


christo


Hello,

your problem is in the system function. Read the 'man awk' carefuly and you will see that this function is exactly the same as the system C-function: it forks a new copy of Bourne shell which gets your string to interpret.

So your process generates a child proces, whose standard output the father process cannot see. You get the output from the system command on your terminal, no matter how you try to redirect it.

The only possibility how to get output from 'top' (and not only top but any command) to your awk script proces is as follows:

CMD="top -p \"3496\" "
while (CMD | getline > 0) {
print $0
# do any processing of the line here
}
close(CMD)

The same holds for all flavours of Unix.

Quite simple if you know it, isn't it? I have made the same error many years ago, too....

Regards

Reply With Quote
  #5  
Old May 19th, 2006, 08:04 AM
ripunjaytripath ripunjaytripath is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 9 ripunjaytripath User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 38 sec
Reputation Power: 0
A confusion

Excuse me, but I have a doubt in the reson that has been given by zlutovsky. Even if a process forks or uses a fork-exec pair to create a child process, the child process still accesses the same control terminal and this is true for shell also. So we can easily direct the output of top command to any file. But the problem lies in the fact that, probably the format of the output of top command may not exactly match the format that is stored in the file. This is due to different kind of interpretation by the drivers(terminal driver in case when output is done on stdout).






your problem is in the system function. Read the 'man awk' carefuly and you will see that this function is exactly the same as the system C-function: it forks a new copy of Bourne shell which gets your string to interpret.

So your process generates a child proces, whose standard output the father process cannot see. You get the output from the system command on your terminal, no matter how you try to redirect it.

The only possibility how to get output from 'top' (and not only top but any command) to your awk script proces is as follows:

CMD="top -p \"3496\" "
while (CMD | getline > 0) {
print $0
# do any processing of the line here
}
close(CMD)

The same holds for all flavours of Unix.

Quite simple if you know it, isn't it? I have made the same error many years ago, too....

Regards [/QUOTE]

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > help with awk script needed!!!!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway