3

I have input coming from pipe which contains special escape characters. For illustration, the stream looks like this:

printf "foo.bar\033[00m" > file
cat file

How can I remove the trailing .bar\033[00m? I have tried the following, but that does not work:

cat file | sed 's/\.bar\033[00m//'
3

If you want in the output just foo then

printf '%s' 'foo.bar\033[00m' | sed 's/\.bar\\033\[00m//'

Notice that both \ and [ has to escaped by another \. Additionally I've added '%s' to printf to print the input characters as literal string, otherwise \033[ could be interpreted as escape code followed by ANSI colour.

After question edit: If your file contains control characters such as

printf "foo.bar\033[00m" > file

then to replace it write the following:

sed 's/\.barCtrl-vESC\[00m//'

After hit Ctrl-vESC you should see ^[ on the screen, but not literal ^ and [ (two characters), but one single control character.

  • This is right. Adding '%s' is crucial in this case to avoid the special interpretation of \033[. – mkc Dec 7 '14 at 20:01
  • but the printf was just for illustration. As I said, my input is coming from pipe, not from printf. I need a solution which will work even without printf, such as: cat file | sed 's/\.bar\\033\[00m//' – Martin Vegter Dec 7 '14 at 20:07
  • And what is the problem? sed 's/\.bar\\033\[00m//' file should work just fine, doesn't it? Perhaps you should add g just before final ' to replace all occurrences of such pattern. – jimmij Dec 7 '14 at 20:14
  • I have used cat file | sed ... as an example to show why printf does not work. But I need to use sed not on file, but on input coming from pipe. Sorry if my example was confusing. – Martin Vegter Dec 7 '14 at 20:30
  • @MartinVegter From the edit of your question I believe that your file (or input stream) in fact doesn't have a string \033[, but rather control character. In such case write the following: cat file | sed 's/\.bar Ctrl-v ESC \[00m//'. Here Ctrl-v ESC inputs escape control character, you should see ^[ on the screen. Please report if this is the case so I will modify the answer. – jimmij Dec 7 '14 at 20:41
1

You need to escape the [ and \ characters with a backslash:

printf '%s' "foo.bar\033[00m" | sed 's/\.bar\\033\[00m//'

Alternatively, if you want to remove everything after the dot character in the string:

printf "foo.bar\033[00m" | sed 's/\..*//'
  • this actually does not work. It does not remove anything. – Martin Vegter Dec 7 '14 at 20:02
  • @MartinVegter I edited the answer with '%s' inspired by the answer from jimmij for correctness. – mkc Dec 7 '14 at 20:09
  • thanks, but unfortunately this does not help me. My input is coming from pipe, not from printf. I need a sed command which will work like this: cat file | sed 's/\.bar\\033\[00m//'. The currect sed syntax does not work for this. – Martin Vegter Dec 7 '14 at 20:10
  • @MartinVegter Would this work: cat file | sed 's/\.bar.*//' – mkc Dec 7 '14 at 20:16
  • no. I don't want to remove any occurrence of \.bar.*. I only need to remove if it is at the end (followed by ANSI color code) – Martin Vegter Dec 7 '14 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.