SED to remove a line above and lines below. | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

SED to remove a line above and lines below.

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-08-2010
chiewming chiewming is offline
Registered User
 
Join Date: Nov 2010
Last Activity: 18 November 2010, 1:49 AM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
SED to remove a line above and lines below.

Hi All,

I need help on removing lines in a text file.

Sample file :
Quote:

Client=0x 0
IPAddress=113.211.45.33
Client=0
ClientV=0
........
Uncomp=0
Comp=0
When there is a match ip for IPAddress in my `cat ip.out`, proceed delete line above until string "Comp" is found.

Thank you very much.

---------- Post updated at 12:56 AM ---------- Previous update was at 12:53 AM ----------

I found very useful SED in this forum but i got no idea how to manipulate it.


Code:
sed -rn '/(0\.0000 *){6}/{n;n;x;d};x;1d;p;${x;p}'


Last edited by Scott; 11-08-2010 at 03:23 AM.. Reason: Code tags
Sponsored Links
    #2  
Old 11-08-2010
michaelrozar17's Avatar
michaelrozar17 michaelrozar17 is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 24 November 2014, 9:59 PM EST
Location: IN
Posts: 892
Thanks: 14
Thanked 182 Times in 173 Posts
To be precise could you pls post the desired output from the above sample file..
Sponsored Links
    #3  
Old 11-08-2010
birei birei is offline
Registered User
 
Join Date: Oct 2010
Last Activity: 24 June 2014, 5:36 AM EDT
Location: Bilbao, Spain
Posts: 628
Thanks: 8
Thanked 173 Times in 171 Posts
Hi,

According to michaelrozar17, I find the post not clear enought. Even so I will make a try:

Code:
$ cat infile
Client=0x 0
IPAddress=113.211.45.33
Client=0
ClientV=0
........
Uncomp=0
Comp=0

$ sed '/^IPAddress=113\.211\.45\.33/I , /^Comp=/I { /^IPAddress=/I! d }' infile
Client=0x 0
IPAddress=113.211.45.33

Regards,
Birei
    #4  
Old 11-11-2010
chiewming chiewming is offline
Registered User
 
Join Date: Nov 2010
Last Activity: 18 November 2010, 1:49 AM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Guy,

Below is the sample output file

Quote:
Client=0x 0
IPAddress=200.200.200.200
Client=0
ClientV=0
........
Uncomp=0
Comp=0

Client=0x 0
IPAddress=113.211.45.33
Client=0
ClientV=0
........
Uncomp=0
Comp=0

Client=0x 0
IPAddress=10.10.10.10
Client=0
ClientV=0
........
Uncomp=0
Comp=0
TO

Quote:
Client=0x 0
IPAddress=200.200.200.200
Client=0
ClientV=0
........
Uncomp=0
Comp=0

Client=0x 0
IPAddress=10.10.10.10
Client=0
ClientV=0
........
Uncomp=0
Comp=0
I get it resolved by combining all rows and add a newline whenever there is Client string and finally grep -v.

But I still keen to know how to doit.

Thank you in advance.
Sponsored Links
    #5  
Old 11-11-2010
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 28 November 2014, 6:46 AM EST
Location: The Netherlands
Posts: 7,720
Thanks: 123
Thanked 538 Times in 508 Posts
Try this:

Code:
awk '!/113.211.45.33/' RS= ORS="\n\n" file

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Remove duplicate lines (the first matching line by field criteria) joggdial3000 Shell Programming and Scripting 6 05-03-2010 12:18 PM
Remove All Lines Between Two Unique Lines Grizzly Shell Programming and Scripting 2 02-26-2010 06:08 AM
Break lines up into single lines after each space in every line lewk Shell Programming and Scripting 7 10-14-2009 10:33 AM
shell script to remove all lines from a file before a line starting with pattern raksha.s Shell Programming and Scripting 2 03-29-2009 08:13 AM
Remove header(first line) and trailer(last line) in ANY given file madhunk Shell Programming and Scripting 2 03-13-2006 03:36 PM



All times are GMT -4. The time now is 11:43 AM.