sed or awk delete character in the lines before and after the matching line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed or awk delete character in the lines before and after the matching line
# 1  
Old 02-07-2012
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:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 02-07-2012 at 06:56 PM.. Reason: code tags, please!
# 2  
Old 02-07-2012
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

# 3  
Old 02-07-2012
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:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 02-07-2012 at 06:58 PM.. Reason: code tags, please!
# 4  
Old 02-07-2012
Quote:
Originally Posted by Scrutinizer
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.
# 5  
Old 02-08-2012
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

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 02-08-2012
This worked perfectly. Thank you very much for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Seemingly simple sed, delete between matching lines

There are many matching blocks of text in one file that need to be deleted. This example below is one block that needs to be either deleted or replaced with an empty line. This text below is the input file. The ouput file should be empty Searching Checks. Based on search criteria name: Value :... (2 Replies)
Discussion started by: bash_in_my_head
2 Replies

2. Shell Programming and Scripting

How to delete a character using sed and or awk?

Hi, 1/ i have file test.txt 1 Jul 28 08:35:29 2014-07-28 Root::UserA 1 Jul 28 08:36:44 2014-07-28 Root::UserB i want to delete the seconds of the file, and the Root:: and the output will be: 1 Jul 28 08:35 2014-07-28 UserA 1 Jul 28 08:36 2014-07-28 UserB 2/i have another file test2.txt:... (8 Replies)
Discussion started by: fxsme
8 Replies

3. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

4. Shell Programming and Scripting

Need awk/sed command to pull out matching lines from a block

Hi, In order to make our debugging easier in log files, I need this script. My log file will be structured like this : ------Invoking myfile -param:start_time=1371150900000 -param:end_time=1371151800000 for 06/14/2013 <multiple lines here> ..... - Step Sybase CDR Table.0 ended... (3 Replies)
Discussion started by: Lakshmikumari
3 Replies

5. Shell Programming and Scripting

Sed: delete on each line before a character and after a character

Hi there, A total sed noob here. Is there a way using sed to delete everything before a character AND after another character on each line in a file? The deletion should also delete the indicating characters(here: an opening and a closing parenthesis). The original file would look like... (3 Replies)
Discussion started by: bnbsd
3 Replies

6. Shell Programming and Scripting

Sed/awk to delete single lines that aren't touching other lines

Hello, I'm trying to figure out how to use sed or awk to delete single lines in a file. By single, I mean lines that are not touching any other lines (just one line with white space above and below). Example: one two three four five six seven eight I want it to look like: (6 Replies)
Discussion started by: slimjbe
6 Replies

7. Shell Programming and Scripting

Delete character in determinate position with sed/awk

Hello. I'm trying to delete one character in determinate position. Example: qwEtsdf123Ecv34 <delete character in positión 3> Result: qwtsdf123Ecv34 Plase, help me. Thanks (4 Replies)
Discussion started by: maria_florencia
4 Replies

8. Shell Programming and Scripting

sed/awk: Delete matching words leaving only the first instance

I have an input text that looks like this (comes already sorted): on Caturday 22 at 10:15, some event on Caturday 22 at 10:15, some other event on Caturday 22 at 21:30, even more events on Funday 23 at 11:00, yet another event I need to delete all the matching words between the lines, from... (2 Replies)
Discussion started by: GrinningArmor
2 Replies

9. Shell Programming and Scripting

sed find matching pattern delete next line

trying to use sed in finding a matching pattern in a file then deleting the next line only .. pattern --> <ad-content> I tried this but it results are not what I wish sed '/<ad-content>/{N;d;}' akv.xml > akv5.xml ex, <Celebrant2First>Mickey</Celebrant2First> <ad-content> Minnie... (2 Replies)
Discussion started by: aveitas
2 Replies

10. Shell Programming and Scripting

Matching multiples of a single character using sed and awk

Hi, I have a file 'imei_01.txt' having the following contents: $ cat imei_01.txt a123456 bbr22135 yet223 where I want to check whether the expression 'first single alphabet followed by 6 digits' is present in the file (here it is the first record 'a123456') I am using the following... (5 Replies)
Discussion started by: royalibrahim
5 Replies
Login or Register to Ask a Question