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"
Edit 1:
Thanks for all the useful answers. But the one I marked as correct is the one which can be typed on terminal in short time and with fewer hassles (simplicity) so that I(syadmins) can do the operations very fast without making the systems downtime more.
printf "\n"
is wrong. It doesn't solve the trailing blank problem and it's hard-coding the value you hopeORS
will have (\n
) instead of simply usingORS
as would occur if you usedprint ""
instead ofprintf "\n"
. If the OP cares about an added blank or a mising newline then the right way to do this with awk is what I show at https://unix.stackexchange.com/a/594059/133219 - use the valueOFS
as appropriate and end with the value ofORS
as appropriate. – Ed Morton Jun 21 '20 at 13:41\r\n
and\0
are common ones that come to mind but beyond that philosophically - awk has a variableORS
that contains the value of the record terminator, any time you have a variable for a specific purpose writing code that then hardcodes the value of that variable is generally bad, especially if it's lengthier code (printf "\n"
) than code that just uses the variable (print ""
). – Ed Morton Jun 21 '20 at 14:19