Home > database >  Interpret a string with escape characters from stdin
Interpret a string with escape characters from stdin

Time:10-13

if I have a file like:

hello \n world

how can I print the contents of the file with the escape sequences interpreted, so in the terminal you see:

hello 
 world

I've tried cat file | xargs -I{} echo {} but that prints hello n world

CodePudding user response:

printf's %b format specifier expands escape sequences:

$ printf '%b\n' "$(cat file)"
hello
 world

To read from stdin an unadorned cat will do:

$ printf '%b\n' "$(cat)"
hello
 world

CodePudding user response:

I think

echo 'hello\\nworld' | xargs -I {} printf "%b" "{}"

is what you're after. But that requires you to have some control over the text sent over the pipe since it requires double backslashes.

  • Related