LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-16-2011, 07:00 AM   #1
jkeertir
Member
 
Registered: Mar 2008
Posts: 75

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,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
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,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
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: 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, 09:27 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
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: 228

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


Reply

Tags
awk, grep, sed



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 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

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

All times are GMT -5. The time now is 10:40 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