How to unescape text file?

I have a log.txt file with this content:

 systemd[1]: Starting dracut cmdline hook...\r\n[ 8.759274] systemd[1]: 

When I do cat log.txt output:

 systemd[1]: Starting dracut cmdline hook...\r\n[ 8.759274] systemd[1]: 

The expected output is 2 lines ( \r\n to replace with a visible new line):

 systemd[1]: Starting dracut cmdline hook... [ 8.759274] systemd[1]: 

How to do this on a bash terminal?

+5
source share
3 answers

Without any external command, you can do this by using printf '%b\n' instead of cat :

 printf '%b\n' "$(<log.txt)" systemd[1]: Starting dracut cmdline hook... [ 8.759274] systemd[1]: 
  • %b extends backslash escape sequences in the corresponding argument
  • $(<log.txt) - bash directive to get the contents of log.txt
+7
source

You can use echo -e for this:

 echo -e $(<log.txt) 
+3
source

You can also use sed:

 $ cat log.txt | sed -e 's/\\n/\n/' -e 's/\\r//' 

If this does not work, try

 $ cat log.txt | sed -e 's/\\r//' -e 's/\\n/\ /' 

that is, press enter after the last backslash and end the command on the next line.

0
source

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


All Articles