Replace value except end of line

So, I have a text file that contains ASCII STX characters (targetable with \ x02) sprinkled all over. I want to delete them if they are NOT at the end of the line.

# This removes all STX characters
sed -i 's/\x02//g' foo.txt

# This removes STX characters that are at the end of a line
sed -i 's/\x02$//g' foo.txt

How to delete a character if it is NOT at the end of a line?

+4
source share
1 answer

You can match and commit any char after one or more characters that need to be removed in order to recover them later using the link:

sed -i 's/\x02\{1,\}\(.\)/\1/g' foo.txt

Here \x02\{1,\}corresponds to 1 or more characters that need to be deleted, \(.\)will match and capture any char to group 1 and \1will restore the captured char result.

+3

Source: https://habr.com/ru/post/1669310/


All Articles