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

2 Answers 2

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.

1
  • 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 Commented Feb 27, 2017 at 8: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
4
  • 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. Commented Feb 27, 2017 at 14:37
  • Can you rephrase your question to make it unambiguous? Commented Feb 28, 2017 at 9: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 Commented Feb 28, 2017 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. Commented Feb 28, 2017 at 11:06

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.