Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



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

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-07-2012
Registered User
 

Join Date: Feb 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
sed or awk delete character in the lines before and after the matching line

Sample file:

Code:
This is line one,
this is another line,
this is the PRIMARY INDEX line
l ;
This is another line

The command should find the line with “PRIMARY INDEX” and remove the last character from the line preceding it (in this case , comma) and remove the first character from the line following it (in this case l )

The output should look like:

Code:
This is line one,
this is another line
this is the PRIMARY INDEX line
;
This is another line


Thank you for you help
Moderator's Comments:

Please use code tags when posting data and code samples!

Last edited by vgersh99; 02-07-2012 at 05:56 PM.. Reason: code tags, please!
Sponsored Links
    #2  
Old 02-07-2012
Scrutinizer's Avatar
mother ate her
 

Join Date: Nov 2008
Location: Amsterdam
Posts: 5,371
Thanks: 80
Thanked 1,107 Times in 1,011 Posts
Try something like this:

Code:
awk '/PRIMARY INDEX/{sub(/.$/,x,p);print p RS $0;getline;sub(/^./,z);print;next}p{print p}{p=$0}' infile

Sponsored Links
    #3  
Old 02-07-2012
Registered User
 

Join Date: Feb 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Thank you for your quick respose. It almost works.
The only problem is it replaces the last line with the line before the matching line at the end. The two lines "this is another line" in my previous sample are not always the same (I should have used different lines).
For example if the sample file is:

Code:
This is line one,
this is another line
this is the PRIMARY INDEX line
 ;
this is yet a different line

That command produces:

Code:
This is line one,
this is another line
this is the PRIMARY INDEX line
 ;
this is another line

This replaces the "this is yet a different line" with "this is another line". Is there a way to modify the awk command to not replace the last line?

Thank you.
Moderator's Comments:

Please use code tags when posting data and code samples!

Last edited by vgersh99; 02-07-2012 at 05:58 PM.. Reason: code tags, please!
    #4  
Old 02-07-2012
Registered User
 

Join Date: Jan 2012
Posts: 91
Thanks: 63
Thanked 1 Time in 1 Post
Quote:
Originally Posted by Scrutinizer View Post
Try something like this:

Code:
awk '/PRIMARY INDEX/{sub(/.$/,x,p);print p RS $0;getline;sub(/^./,z);print;next}p{print p}{p=$0}' infile

Could you please explain the logic?

Thanks.
Sponsored Links
    #5  
Old 02-08-2012
Scrutinizer's Avatar
mother ate her
 

Join Date: Nov 2008
Location: Amsterdam
Posts: 5,371
Thanks: 80
Thanked 1,107 Times in 1,011 Posts
Like this?

Code:
awk '/PRIMARY INDEX/{sub(/.$/,x,p);print p RS $0;if(getline){sub(/^./,x);print}p=x;next}
     p{print p}{p=$0}END{if(p)print p}' infile

The Following User Says Thank You to Scrutinizer For This Useful Post:
KC_Rules (02-08-2012)
Sponsored Links
    #6  
Old 02-08-2012
Registered User
 

Join Date: Feb 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
This worked perfectly. Thank you very much for your help.
Sponsored Links
Reply

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
Delete lines upto matching path variable Bashingaway Shell Programming and Scripting 6 10-07-2010 08:43 PM
delete lines in file matching a pattern stumpyuk Shell Programming and Scripting 2 03-24-2009 02:06 PM
sed find matching pattern delete next line aveitas Shell Programming and Scripting 2 12-22-2008 12:54 PM
how to delete a matching line from a file codeman007 Shell Programming and Scripting 5 12-17-2008 01:58 AM
how to delete line with matching text and line immediately after orahi001 UNIX for Dummies Questions & Answers 6 01-14-2008 11:34 PM



All times are GMT -4. The time now is 04:37 AM.