Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

SED to remove a line above and lines below.

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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.

LinuxHi 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}'

Linux

Last edited by Scott; 11-08-2010 at 02:23 AM.. Reason: Code tags
Sponsored Links
    #2  
Old Unix and Linux 11-08-2010
michaelrozar17's Unix or Linux Image
michaelrozar17 michaelrozar17 is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 23 July 2015, 8:19 AM EDT
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 Unix and Linux 11-08-2010
birei birei is offline
Registered User
 
Join Date: Oct 2010
Last Activity: 4 July 2015, 1:29 PM 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 Unix and Linux 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 Unix and Linux 11-11-2010
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 1 September 2015, 9:23 AM EDT
Location: The Netherlands
Posts: 7,732
Thanks: 129
Thanked 549 Times in 516 Posts
Try this:

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

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 11:18 AM
Remove All Lines Between Two Unique Lines Grizzly Shell Programming and Scripting 2 02-26-2010 05:08 AM
Break lines up into single lines after each space in every line lewk Shell Programming and Scripting 7 10-14-2009 09: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 07:13 AM
Remove header(first line) and trailer(last line) in ANY given file madhunk Shell Programming and Scripting 2 03-13-2006 02:36 PM



All times are GMT -4. The time now is 06:18 PM.