Shell Script – Convert Multi Lines to Single Line with Spaces and Quotes

shelltext processing

How can I convert multi lines to a single line with spaces and quotes
using awk or tr or any other tool which makes it simple
(but not for loops)?

$ cat databases.txt
Wp_new
Frontend DB
DB_EXT

Expected:

$ cat databases.txt
"Wp_new" "Frontend DB" "DB_EXT"

Best Answer

With sed + paste

$ sed 's/.*/"&"/' databases.txt
"Wp_new"
"Frontend DB"
"DB_EXT"

$ sed 's/.*/"&"/' databases.txt | paste -sd' ' -
"Wp_new" "Frontend DB" "DB_EXT"

Or, just paste (courtesy https://unix.stackexchange.com/a/593240)

$ <databases.txt paste -d '"' /dev/null - /dev/null | paste -sd' ' -
"Wp_new" "Frontend DB" "DB_EXT"


If input has empty lines that should be ignored:

$ cat ip.txt
Wp_new

Frontend DB



DB_EXT
$ sed -n 's/..*/"&"/p' ip.txt | paste -sd' ' -
"Wp_new" "Frontend DB" "DB_EXT"
Related Question