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

Sample file:

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:

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

Thank you for you help

Try something like this:

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

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:

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

That command produces:

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.

Could you please explain the logic?

Thanks.

Like this?

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
1 Like

This worked perfectly. Thank you very much for your help.