0

I am trying to delete x02 character from a file, I can easily delete x02 from a file using sed -i 's/\x2//g' command. Problem here is I don't want to delete x02 character which immediately starts with I. Except that I want to delete all other x02 characters.

Example:

File data:

I^A12^Agop^Bal^BI^A3^B^B4^Aramu^BI^A56^Asubbu^BI^A78^Asai^B

Expected output:

I^A12^Agopal^BI^A34^Aramu^BI^A56^Asubbu^BI^A78^Asai^B
user
  • 28,901
Ram
  • 11

2 Answers2

1

You could explicitly match anything but I:

sed -i 's/\x02\([^I]\)/\1/g

This matches any pair of characters which are any character other than I preceded by \x02 and replaces it with the second character.

bwduncan
  • 196
  • Thanks Gilles, It was working fine if there is one x02. If I have more then one x02 it not working fine. In the above example, it was unable to delete x02 character between 3 and 4 I^A12^Agopal^BI^A3^B4^Aramu^BI^A56^Asubbu^BI^A78^Asai^B – Ram Feb 27 '17 at 08:57
0
sed -e '
  s/\(\x02\)\1\1*//g;   # erase 2 or more consecutive STX chars
  s/\x02\([^I]\)/\1/g;  # erase the STX only if followed by a non-I char
' yourfile
  • Thanks, it was working fine. Just out of curios, just want to know. What if I want to check for both I and /x01 character instead of only I before deleting /x02. – Ram Feb 27 '17 at 14:37
  • Can you rephrase your question to make it unambiguous? – Rakesh Sharma Feb 28 '17 at 09:16
  • In below example, if I want to delete /x02 which immediately starts with I and \x02 I^A12^Agop^Bal^BI^A3^BI^B4^Aramu^BI^A56^Asubbu^BI^A78^Asai^B Here I don't want to delete I in 3^BI^B4 as it doesn't have immediate I and /x02 – Ram Feb 28 '17 at 10:57
  • Before you post, read it to ensure it is very clear. For example, your statement, Here I don't want to delete I in 3^BI^B4 as it does't have immediate I which contradicts with your assertion in the beginning that you want to delete /x02 which immediately starts with I` So it's not clear whether you are focussing on deleting I or deleting \x02. – Rakesh Sharma Feb 28 '17 at 11:06