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.
ps -ly | while
read c1 c2 c3 c4 c5 c6 c7 c8 c9 c10
do
echo $c6 $c7 $c8 | sed '1d'
done
I'm trying to delete the first line, the header, so that only the numbers are output. The thing is that sed there doesn't work. And I've no idea why. Normally if I do sed '1d' file.txt, then the first line won't show up. Why is it different here?
I know that echo normally turns columns into a single line with words separated by spaces, but here this is not the case. Echo will still show columns (that's another thing I don't really understand - maybe it's because of the while loop? Though it shouldn't be).
If I try echo $c6 | grep -E [[:digit:]], then I get the correct output. But I'd like to specifically delete the first line.
I was actually following this lynda course on bash, and I did have the solution, but I wanted to understand some basic stuff before looking at it and, of course, trying it myself.
The idea was to do the sum of all the numbers that showed under RSS and SZ. This is what they came up with:
Code:
#!/bin/bash
n=1
ps -ly | while
read c1 c2 c3 c4 c5 c6 c7 c8 c9 c10
do
if ((n>1))
then
((rss=rss+c8))
((sz=sz+c9))
echo rss=$rss sz=$sz
fi
((n++))
done
Which is still a little bit hard to thoroughly understand, even though I could easily reproduce it (they're few lines, so yeah ). What I don't understand, though, is, now that you've mentioned it, how the loop indeed skips the first line. The guy in the video says that he's actually using the "n" variable to do it. But I don't see how that affects the script.
ps -ly | while
read c1 c2 c3 c4 c5 c6 c7 c8 c9 c10
do
echo $c6 $c7 $c8 | sed '1d'
done
I'm trying to delete the first line, the header, so that only the numbers are output. The thing is that sed there doesn't work. And I've no idea why. Normally if I do sed '1d' file.txt, then the first line won't show up. Why is it different here?
The echo and sed commands are invoked separately for each iteration of the loop. Each echo command writes a single line, and each sed command deletes the first (and only) line that it sees.
If you want sed to filter the combined output from the loop, write it that way:
Code:
ps -ly | while read c1 c2 c3 c4 c5 c6 c7 c8 c9 c10
do
echo $c6 $c7 $c8
done | sed '1d'
What I don't understand, though, is, now that you've mentioned it, how the loop indeed skips the first line. The guy in the video says that he's actually using the "n" variable to do it. But I don't see how that affects the script.
The magic is the 'if' statement. 'n' is originally set to 1 and when the first line is read, the one you want to get rid of, 'n' is still 1 so the 'if' skips this line as the test says 'n>1'.
After skipping the first lot of output, 'n' is increased before the next iteration of the loop, hence now 'n' is 2 or greater and so all the lines being processed now will enter the 'if' clause as they
pass the test.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.