In bash, how can I display the contents of a file with multiple lines as a single line, where new lines are displayed as \n.
\n
Example:
$ echo "line 1 line 2" >> file.txt
I need to get the contents as "line 1\nline2"using bash commands.
"line 1\nline2"
I tried to use combinations cat/printf/echowithout success.
cat/printf/echo
You can use bash printfto get something close:
printf
$ printf "%q" "$(< file.txt)" $'line1\nline2'
and in bash4.4 there is a new parameter extension operator to get the same:
bash
$ foo=$(<file.txt) $ echo "${foo@Q}" $'line1\nline2'
$ cat file.txt line 1 line 2 $ paste -s -d '~' file.txt | sed 's/~/\\n/g' line 1\nline 2
paste, say ~ ~ \n sed.
paste
~
sed
stdin ...
:
cat file|tr '\n' ' '
file - \n. .
, , .
cat file|tr '\n' ' ' >> file2
: Bash
'\n' 2 echo -n
echo -n "line 1 line 2" > file.txt od -cv file.txt 0000000 l i n e 1 \n l i n e 2 sed -z 's/\n/\\n/g' file.txt line 1\nline 2
'\n' 2
echo "line 1 line 2" > file.txt od -cv file.txt 0000000 l i n e 1 \n l i n e 2 \n sed -z 's/\n/\\n/g' file.txt line 1\nline 2\n
$ hexdump -v -e '/1 "%_c"' file.txt ; echo line 1\nline 2\n $ od -vAn -tc file.txt l i n e 1 \n l i n e 2 \n
Source: https://habr.com/ru/post/1661520/More articles:What is the best way to transfer data and export an object? - jsonЗахват системы или звука приложения с помощью AudioKit - iosEvaluating Promt in a Python interactive shell before displaying a tooltip - pythonBash: How to use the operator parameter extension $ {parameter @operator}? - bashHow to play an audio file in response mode? First of all in android - javascriptMat to Bitmap in jni cpp file for Android - c ++Create a workspace from Powershell as part of the VS Team Services assembly - tfsMore efficient way to update an item in a list in Elm? - elmCan I clear a “dynamic web page” with beautifulsoup? - pythonОчистите серию таблиц с помощью BeautifulSoup - pythonAll Articles