LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 09-30-2016, 04:58 AM   #1
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Rep: Reputation: 103Reputation: 103
sed '1d' not showing after piping echo $variable


I've got this script:

Code:
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.
 
Old 09-30-2016, 05:03 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,802

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
sed definitely works, just your script was not constructed properly.
Code:
ps -ly | while read ...
do
echo | sed   <<< here echo will print one single line and sed will delete the first line
done
what you really need is either use sed before the while or after the loop:
Code:
ps -ly | sed 1d | while read
...
or
Code:
ps -ly | while read ...
do
...
done | sed 1d
(not tested)
 
1 members found this post helpful.
Old 09-30-2016, 05:10 AM   #3
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
So the reason why echo shows columns instead of lines is because it echoes a line at a time and the newline is added by the while loop, right?
 
Old 09-30-2016, 05:18 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,802

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
echo by default adds a newline at the end, it is not the while loop.
echo does not print columns but what you specified to print: $c6 $c7 $c8
 
Old 09-30-2016, 06:41 AM   #5
c0wb0y
Member
 
Registered: Jan 2012
Location: Inside the oven
Distribution: Windows
Posts: 417

Rep: Reputation: 74
Try this:

Code:
[c0wb0y@centOS ~]$ ps -ly | tail -n +2
S  1000  2795  1964  0  80   0  2180 28871 wait   pts/1    00:00:00 bash
R  1000  3504  2795  0  80   0  1460 37227 -      pts/1    00:00:00 ps
D  1000  3505  2795  0  80   0   660 28871 sleep_ pts/1    00:00:00 bash
 
Old 09-30-2016, 08:32 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,780

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
You can pipe into an explicit block with an extra read for the first line; the while will loop over the remainder
Code:
ps -ly | {
  read header
  while read c1 c2 c3 c4 c5 c6 c7 c8 rest
  do
    ...
  done
}
 
Old 09-30-2016, 08:49 AM   #7
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
Quote:
Originally Posted by MadeInGermany View Post
You can pipe into an explicit block with an extra read for the first line; the while will loop over the remainder
Code:
ps -ly | {
  read header
  while read c1 c2 c3 c4 c5 c6 c7 c8 rest
  do
    ...
  done
}
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.
 
Old 09-30-2016, 08:57 AM   #8
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by vincix View Post
I've got this script:

Code:
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'
 
Old 09-30-2016, 01:46 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
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.

Hope that helps.
 
1 members found this post helpful.
Old 09-30-2016, 07:46 PM   #10
c0wb0y
Member
 
Registered: Jan 2012
Location: Inside the oven
Distribution: Windows
Posts: 417

Rep: Reputation: 74
How about this:

Code:
ps -ly | awk 'BEGIN { RSS=0; SZ=0 }; NR>1 {print $2, $3, $4, $5, $6, $7, $8; RSS+=$8; SZ+=$9} END { print "RSS =", RSS, "SZ =", SZ }'
 
1 members found this post helpful.
  


Reply



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
Piping cat output to variable? zizou86 Programming 12 01-13-2010 03:17 PM
sed & piping (solved) bpwoods Linux - General 1 08-19-2009 03:36 PM
Piping output fm cut into sed wtaicken Programming 7 12-09-2008 10:54 AM
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 03:44 PM
function showing a list of variable and value: (dynamic variable name) budhax Linux - Newbie 1 09-19-2008 07:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:06 AM.

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