LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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
 
LinkBack Search this Thread
Old 02-16-2011, 07: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, 07:06 AM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,443

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Code:
grep -vA1 "#2" foo.txt
Something like that?
 
Old 02-16-2011, 07: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,500

Rep: Reputation: 285Reputation: 285Reputation: 285
This will do it:

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

Rep: Reputation: 60
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, 09:27 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Mint
Posts: 5,403

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

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

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
Trackbacks are Off
Pingbacks are On
Refbacks are 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 03:45 AM
Using CAT / GREP to remove a set of lines from a file wesgarner Linux - Newbie 13 11-14-2009 10: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 01:16 AM
script to grep lines out of a file qipman Linux - General 5 09-26-2007 12:01 PM
Iptables, where do i store NAT lines? d33pdream Linux - Software 1 03-18-2003 12:20 AM


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

Main Menu
 
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
Open Source Consulting | Domain Registration