LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-07-2007, 11:24 PM   #1
yitzle
Member
 
Registered: Aug 2006
Posts: 50

Rep: Reputation: 15
Echo every other line


I've got some text that I'm operating on through a pipe, eg
cat - | grep "someString" | wc -l
On thing I'd like to do is weed out every other line.
Is there some way to do this?
I was thinking of somehow using sed and changing the separator to "", the telling it to run s,([^\n]\n)[^\n]\n,\1,

Would that work? How do I change the separator?
 
Old 11-08-2007, 01:47 AM   #2
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Not sure why you would want to just show every other line, however have you thought of looping:

Code:
cat - |grep "something"|while read l_line
do
   # Perform a test of some sort before echoing!
   echo $l_line
done
Suspecting homework therefore this is not a complete solution.

If this is not homework please explain why you want to show every other line (what this is for in a real world example)
 
Old 11-08-2007, 02:37 AM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by yitzle View Post
I've got some text that I'm operating on through a pipe, eg
cat - | grep "someString" | wc -l
On thing I'd like to do is weed out every other line.
Is there some way to do this?
I was thinking of somehow using sed and changing the separator to "", the telling it to run s,([^\n]\n)[^\n]\n,\1,

Would that work? How do I change the separator?
Way too complicated...

man sed
/step



Cheers,
Tink
 
Old 11-08-2007, 02:51 AM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Cheers Tink

I didn't know sed could do that so easily!

You learn something everyday.
 
Old 11-08-2007, 12:52 PM   #5
yitzle
Member
 
Registered: Aug 2006
Posts: 50

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Disillusionist View Post
Not sure why you would want to just show every other line, however have you thought of looping:

Code:
cat - |grep "something"|while read l_line
do
   # Perform a test of some sort before echoing!
   echo $l_line
done
Suspecting homework therefore this is not a complete solution.

If this is not homework please explain why you want to show every other line (what this is for in a real world example)
Its not homework. I got output from a turn based game/program that shows the result of me vs opponent. Every other line is me, round next.
I want to count the occurances of a specific result of the me lines.
To flesh out your example, you could use a binary toggle to print every other time the loop runs.
Though if there was a course that taught this sort of stuff, I'd love to take it...
Quote:
Originally Posted by Tinkster View Post
Way too complicated...

man sed
/step

Cheers,
Tink
Neat! Thanks!
I didn't even know you could search man (or less, I guess) like that!

One of these days, I'll have to learn sed/awk in detail.
 
Old 11-08-2007, 01:00 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by yitzle View Post
Neat! Thanks!
I didn't even know you could search man (or less, I guess) like that!

One of these days, I'll have to learn sed/awk in detail.
You're most welcome, and "Yes, you ought to" <BEG>

power at your finger tips ;}



Cheers,
Tink
 
Old 11-08-2007, 01:04 PM   #7
yitzle
Member
 
Registered: Aug 2006
Posts: 50

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Tinkster View Post
You're most welcome, and "Yes, you ought to" <BEG>

power at your finger tips ;}


Cheers,
Tink
Once on this topic, what about awk?
Does awk replace sed? Or do they have rather seperate uses?
Should I bother learn both? Which is better to learn?
 
Old 11-08-2007, 01:11 PM   #8
dgar
Member
 
Registered: Jun 2005
Location: Candia, NH
Distribution: Ubuntu, FC, RHE3, RHE4, CentOS
Posts: 121

Rep: Reputation: 15
I was going to resort to an iterator in awk, and then testing even/odd with modulus 2.

Those ancients of UNIX sure thought ahead.
 
Old 11-08-2007, 01:33 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by yitzle View Post
Once on this topic, what about awk?
Does awk replace sed? Or do they have rather seperate uses?
Should I bother learn both? Which is better to learn?
Always depends on the complexity of the task at hand.

Think of awk as a missing link between sed and perl :}

As dgar said ... you could achieve the same result with
awk doing something like (mind you, I don't use an iterator).

awk '{if( NR%2 ){print}}'

The big difference is that awk has a concept of fields
which wasn't required in this instance. Plus it's quite
easy to use multi-line records in awk which can be very
tedious to deal with using sed.


For simpler tasks use the simpler tool (oversimplification).
It has a (marginally) smaller file-size and memory footprint,
so will commonly be somewhat quicker ...

So I'd suggest you learn them both, really.


Cheers,
Tink

Last edited by Tinkster; 11-08-2007 at 01:34 PM.
 
Old 11-08-2007, 02:15 PM   #10
yitzle
Member
 
Registered: Aug 2006
Posts: 50

Original Poster
Rep: Reputation: 15
Thanks for the post! Most informative.
 
Old 11-08-2007, 03:06 PM   #11
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Speakin of awk, your problem is easily solved using it:
Code:
$ for ((i=1;i<101;++i)); do echo $i >>test.txt;done
$ cat test.txt | head
1
2
3
4
5
6
7
8
9
10
$ gawk 'NR%2==0 {print}' test.txt | head
2
4
6
8
10
12
14
16
18
20
 
Old 11-08-2007, 03:24 PM   #12
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by PTrenholme View Post
Speakin of awk, your problem is easily solved using it:
Code:
$ for ((i=1;i<101;++i)); do echo $i >>test.txt;done
$ gawk 'NR%2==0 {print}' test.txt | head
Heh ... slightly more concise than my version above.
But we don't really need the "==0" or the print, either.


Code:
awk 'NR%2 ' file
will do ... it's just not quite as readable if you don't know awk
very well :}



Cheers,
Tink
 
Old 11-08-2007, 04:16 PM   #13
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,140

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
Quote:
Originally Posted by yitzle View Post
I didn't even know you could search man (or less, I guess) like that!
While you're in the mood, add an "-i" subcommand first. I have it as default for things like sed and less.
 
Old 11-08-2007, 04:20 PM   #14
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by syg00 View Post
While you're in the mood, add an "-i" subcommand first. I have it as default for things like sed and less.
/me coughs ...

But that switch has a completely different meaning in those
two tools?

less -> case insensitive search
sed -> in-place editing of the file


Cheers,
Tink
 
Old 11-08-2007, 04:25 PM   #15
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,140

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
Sorry Tink; I haven't got to having my first coffee of the morning ...

Let's try that again - "grep and less".

wanders off to kick the ball for the mutt ... leave the computer alone ...
 
  


Reply

Tags
taylor taylor's+laws



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
PHP and JavaScript: problem with echo()-ing unwanted line breaks Robhogg Programming 3 02-12-2007 07:52 AM
ls | echo, I got blank, why can't echo take the 2nd seat in a pipeline? elinuxqs Linux - Newbie 6 11-24-2006 08:25 AM
Bash Script, no new line for echo command jorisb Linux - General 5 11-05-2005 12:08 AM
Kphone echo (echo echo) scabies Linux - Software 0 10-18-2004 02:59 PM
How to echo variables and commands in one line? hindll01 Programming 1 09-10-2004 06:02 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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

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