How can I join multiple lines into one line, with a separator where the new-line characters were, and avoiding a trailing separator and, optionally, ignoring empty lines?
Example. Consider a text file, foo.txt
, with three lines:
foo bar baz
The desired output is:
foo,bar,baz
The command I'm using now:
tr '\n' ',' <foo.txt |sed 's/,$//g'
Ideally it would be something like this:
cat foo.txt |join ,
What's:
Of course I could write something, or just use an alias. But I'm interested to know the options.
join is a command in Unix and Unix-like operating systems that merges the lines of two sorted text files based on the presence of a common field. It is similar to the join operator used in relational databases but operating on text files. join. Original author(s)
Join Without a Delimiter and With a Single Character Delimiter. A short Bash one-liner can join lines without a delimiter: $ (readarray -t ARRAY < input. txt; IFS=''; echo "${ARRAY[*]}") I cameI sawI conquered!
The join command in UNIX is a command line utility for joining lines of two files on a common field. It can be used to join two files by selecting fields within the line and joining the files on them.
Perhaps a little surprisingly, paste
is a good way to do this:
paste -s -d","
This won't deal with the empty lines you mentioned. For that, pipe your text through grep
, first:
grep -v '^$' | paste -s -d"," -
This sed
one-line should work -
sed -e :a -e 'N;s/\n/,/;ba' file
Test:
[jaypal:~/Temp] cat file foo bar baz [jaypal:~/Temp] sed -e :a -e 'N;s/\n/,/;ba' file foo,bar,baz
To handle empty lines, you can remove the empty lines and pipe it to the above one-liner.
sed -e '/^$/d' file | sed -e :a -e 'N;s/\n/,/;ba'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With