LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 03-13-2008, 08:49 AM   #1
navin29
LQ Newbie
 
Registered: Mar 2008
Posts: 3

Rep: Reputation: 0
Unix command to delete the previous and next line of the searched pattern


Unix command to delete the previous and next line of the searched pattern
 
Old 03-13-2008, 08:58 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
Originally Posted by navin29 View Post
Unix command to delete the previous and next line of the searched pattern
So, what's the question? This looks like homework---the phrase seems to be a cut and paste from an assignment sheet.

Take a look at the grep command--especially the -v and -C options
 
Old 03-13-2008, 09:10 AM   #3
navin29
LQ Newbie
 
Registered: Mar 2008
Posts: 3

Original Poster
Rep: Reputation: 0
I have a file which looks like this:

SOLUTION
ABCD
HELLO
EVERYONE
BYE

I have to search for HELLO pattern and if found ,I have to delete the previous and the next line i.e. the output should look like this..

SOLUTION
BYE
 
Old 03-13-2008, 10:01 AM   #4
trashbird1240
Member
 
Registered: Sep 2006
Location: Durham, NC
Distribution: Slackware, Ubuntu (yes, both)
Posts: 463

Rep: Reputation: 31
man sed

Sed takes regular expressions as address and will perform editing operations between a pair of addresses:

/SOLUTION/,/BYE/s/\(do my homework\) for me/\1 myself/g

should do it.

Joel
 
Old 03-14-2008, 08:31 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
navin29;

Did you look at GREP as I suggested?

Also, when posting homework questions, you will generally get better help by describing the class you are taking, and by showing things you have already tried.

I'm not sure if SED is going to work--although it may be fun to find a way to do it. My favorite SED tutorial here: http://www.grymoire.com/Unix/Sed.html
 
Old 03-14-2008, 10:22 AM   #6
navin29
LQ Newbie
 
Registered: Mar 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Smile

Thankyou for your much valuable suugestions.
I have got the answer to deleting next line but deleting the previous line by a single line command still remains a question.
 
Old 03-14-2008, 10:51 AM   #7
trashbird1240
Member
 
Registered: Sep 2006
Location: Durham, NC
Distribution: Slackware, Ubuntu (yes, both)
Posts: 463

Rep: Reputation: 31
Think about it: a "previous line" is the part of the regular expression followed by a new line and your target address.
 
Old 03-14-2008, 02:23 PM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
Originally Posted by navin29 View Post
Thankyou for your much valuable suugestions.
I have got the answer to deleting next line but deleting the previous line by a single line command still remains a question.
2nd request:
Did you look at the GREP command as I suggested?

Hint: grep will do what you are looking for.
 
  


Reply


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
In Solaris(command Line ), How to get the previous commands? kcarun Solaris / OpenSolaris 22 10-19-2012 04:49 AM
Shell Script to Delete line if pattern exists topcat Programming 22 08-23-2011 05:58 AM
unix shell script:How to delete the first line in a file?? rche3252 Programming 6 03-03-2010 08:32 AM
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 07:17 AM
delete a line containing a pattern and the next line of a text file powah Programming 3 01-31-2007 06:34 PM


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