Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unescape a text file?

Tags:

bash

terminal

cat

I have a file log.txt with this content:

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

When I do cat log.txt output is:

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

Expected output is 2 lines (\r\n to be replaced with visibly new line):

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

How to achieve this in a bash terminal?

like image 588
Joe Avatar asked Feb 08 '18 14:02

Joe


2 Answers

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

printf '%b\n' "$(<log.txt)"

systemd[1]: Starting dracut cmdline hook...
[    8.759274] systemd[1]:
  • %b expands backslash escape sequences in the corresponding argument
  • $(<log.txt) is bash directive to get content of log.txt
like image 132
anubhava Avatar answered Sep 20 '22 12:09

anubhava


You can use echo -e for that:

echo -e $(<log.txt)
like image 27
M. Becerra Avatar answered Sep 21 '22 12:09

M. Becerra