[SOLVED]Wierd AWK behavior / AWK not reading first line.
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.