LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 11-26-2012, 10:36 AM   #1
Involar
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Rep: Reputation: Disabled
[SOLVED]Wierd AWK behavior / AWK not reading first line.


Hello,

I'm having issues with parsing PS output with awk. For some reason the first line is always ignored.

:~$ ps -e -o pid,rss,command | grep tty
850 864 /sbin/getty -8 38400 tty4
854 856 /sbin/getty -8 38400 tty5
861 860 /sbin/getty -8 38400 tty2
862 852 /sbin/getty -8 38400 tty3
864 852 /sbin/getty -8 38400 tty6
923 864 /sbin/getty -8 38400 tty1
2133 836 grep --color=auto tty
:~$ ps -e -o pid,rss,command | grep tty | while read line; do awk '{print $1}'; done
854
861
862
864
923
2135


As you can see the first line (pid in this case) is missing from the output. Is there a way to fix it without having to first save results to a file and then parse the file with awk? Thanks

Last edited by Involar; 11-27-2012 at 01:04 PM.
 
Old 11-26-2012, 11:14 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
You don't need the grep and the while loop, have a look at this:
Code:
ps -e -o pid,command | awk '/tty[0-9]/ { print $1 }'
 
Old 11-26-2012, 11:47 AM   #3
Involar
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Hello,

This was just an example of awk not doing its job. I need the while loop because the script im writing is examining each line and does an action based on what the line contains. Basic example:
:~$ ps -e -o pid,rss,command | grep tty
850 864 /sbin/getty -8 38400 tty4
854 856 /sbin/getty -8 38400 tty5
861 860 /sbin/getty -8 38400 tty2
862 852 /sbin/getty -8 38400 tty3
864 852 /sbin/getty -8 38400 tty6
923 864 /sbin/getty -8 38400 tty1

awk needs to select $2 and if its > 850 a kill -9 needs to performed and logged in a separate log file.
 
Old 11-26-2012, 12:08 PM   #4
rosehosting.com
Member
 
Registered: Jun 2012
Posts: 222

Rep: Reputation: 62
Try with the following one-liner:
Code:
while read pid; do echo "$pid"; done <<< $(ps -e -o pid,rss,command | awk '/tty[0-9]/ { if($2>850) print $2 }')

Quote:
Originally Posted by Involar View Post
Hello,

This was just an example of awk not doing its job. I need the while loop because the script im writing is examining each line and does an action based on what the line contains. Basic example:
:~$ ps -e -o pid,rss,command | grep tty
850 864 /sbin/getty -8 38400 tty4
854 856 /sbin/getty -8 38400 tty5
861 860 /sbin/getty -8 38400 tty2
862 852 /sbin/getty -8 38400 tty3
864 852 /sbin/getty -8 38400 tty6
923 864 /sbin/getty -8 38400 tty1

awk needs to select $2 and if its > 850 a kill -9 needs to performed and logged in a separate log file.
 
Old 11-26-2012, 12:40 PM   #5
Involar
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Hello, i managed to achieve it with the following :
ps -e -o pid,rss,command | grep -v grep | grep tty |
awk '{
if ( $2 > "850" ) {
print( "Killing",$1 )
system( "kill -9 "$1 )
system( "echo `date` " $3 " >> /home/asdf/logs/kill.log")
}
}'

Thanks for the help !
 
Old 11-27-2012, 04:38 AM   #6
goossen
Member
 
Registered: May 2006
Location: Bayern, Germany
Distribution: Many
Posts: 224

Rep: Reputation: 41
Here is a tip to avoid the "grep -v grep" part:
Code:
ps -e -o pid,rss,command | grep [t]ty
 
Old 11-27-2012, 08:14 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,564

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
To answer the original question AWK not reading first line, this is completely correct, however, you did tell your script not to pass the first line to awk.

We can assume that the data prior to the while loop is always in tact as the ps and grep work without issue.
So, if we look at just the while loop portion:
Code:
while read line; do 
    awk '{print $1}'; 
done
From here we see that the first read of the data is performed by the read function and hence the first line is now stored in the variable 'line', ie line='850 864 /sbin/getty -8 38400 tty4'.

Then looking at the second line of code, the glaring omission would be that awk has not been delivered any input, file name or string, so it goes out to the next level of input ... stdin

Here it finds the rest of your data waiting to be processed, which it does, starting at line 2 as there is no longer line 1 available due to it already being read.

There are of course better solutions, some already offered, but to solve the problem you asked, you would need to feed the awk the individual lines you are reading in the loop:
Code:
ps -e -o pid,rss,command | grep tty | while read line; do awk '{print $1}' <<< "$line"; done
 
1 members found this post helpful.
Old 11-27-2012, 08:35 AM   #8
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,929

Rep: Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306
what about pgrep -f 'tty[0-9]' ?
 
Old 11-27-2012, 01:04 PM   #9
Involar
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks for the replies. Appreciate it and indeed grail's solution worked.
 
Old 11-28-2012, 10:53 AM   #10
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,564

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
See my signature for how to mark as SOLVED.
 
  


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
[SOLVED] weird awk behavior schneidz Programming 12 08-28-2012 07:39 AM
Wifi and networking - wierd behavior on Fedora 8 algogeek Linux - Hardware 1 05-09-2008 06:26 AM
Wierd Fedora behavior maxcell Linux - General 3 03-01-2007 11:17 AM
ctrl-x wierd behavior in minicom vrod_rink Linux - Software 1 02-02-2006 11:37 PM
Wierd PAM limits.conf behavior Kostko Linux - Software 0 08-26-2003 05:55 AM


All times are GMT -5. The time now is 07:16 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration