|
Search Forums:
|
|||||||
| Forums | Register | Forum Rules | Linux and Unix Links | Man Pages | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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
Last edited by vgersh99; 02-07-2012 at 05:56 PM.. Reason: code tags, please! |
| Sponsored Links | |
|
|
|
#2
|
||||
|
||||
|
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
|
||||
|
||||
|
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.
Last edited by vgersh99; 02-07-2012 at 05:58 PM.. Reason: code tags, please! |
|
#4
|
|||
|
|||
|
Quote:
Thanks. |
| Sponsored Links | |
|
|
#5
|
||||
|
||||
|
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
|
|||
|
|||
|
This worked perfectly. Thank you very much for your help.
|
| Sponsored Links | ||
|
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|