LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 02-16-2011, 08:00 AM   #1
jkeertir
Member
 
Registered: Mar 2008
Posts: 70

Rep: Reputation: 15
grep and store two lines in other file


Dear all,

I have following contents

#1
aaaa
#2
bbb
#3
ccc

I want to grep "#2" and want the output as

#1
aaaa
#3
ccc

Please help me using shell script

With regards,
Keerti
 
Old 02-16-2011, 08:06 AM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,652

Rep: Reputation: 536Reputation: 536Reputation: 536Reputation: 536Reputation: 536Reputation: 536
Code:
grep -vA1 "#2" foo.txt
Something like that?
 
Old 02-16-2011, 08:25 AM   #3
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 328Reputation: 328Reputation: 328Reputation: 328
This will do it:

Code:
grep -v "`grep -A1 '#2' filename`" filename
 
Old 02-16-2011, 08:32 AM   #4
fordeck
Member
 
Registered: Oct 2006
Location: Utah
Posts: 520

Rep: Reputation: 61
If you want to store the output in a new file just add a redirection. For example using AlucardZero's code:

Code:
grep -vA1 "#2" foo.txt > newfile
Regards,

Fordeck
 
Old 02-16-2011, 10:27 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Or if it doesn't have to be grep:
Code:
awk '/2/{getline;next}1' file
 
Old 02-16-2011, 11:46 AM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Sed is another possibility to accomplish that:
Code:
sed -i '/^#2/ {N;d}' file
 
Old 02-16-2011, 08:32 PM   #7
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
Code:
$ ruby -ne 'gets && next if /^#2/;print ' file
#1
aaaa
#3
ccc
 
  


Reply

Tags
awk, grep, sed


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] grep ??? searching a file and outputting 5 lines either side... redhatuser1 Linux - Newbie 2 09-29-2010 04:45 AM
Using CAT / GREP to remove a set of lines from a file wesgarner Linux - Newbie 13 11-14-2009 11:41 AM
how to retrieve some specific set of lines from a file and store it in a char buffer. vigneshinbox Programming 3 04-02-2009 02:16 AM
script to grep lines out of a file qipman Linux - General 5 09-26-2007 01:01 PM
Iptables, where do i store NAT lines? d33pdream Linux - Software 1 03-18-2003 01:20 AM


All times are GMT -5. The time now is 03:10 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration