Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concise and portable "join" on the Unix command-line

Tags:

shell

unix

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:

  1. the most portable, concise, readable way.
  2. the most concise way using non-standard unix tools.

Of course I could write something, or just use an alias. But I'm interested to know the options.

like image 640
butt Avatar asked Dec 15 '11 15:12

butt


People also ask

What is join command in Unix?

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)

How do you join lines in Unix?

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!

What is the command join used for?

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.


2 Answers

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"," - 
like image 54
Michael J. Barber Avatar answered Sep 17 '22 16:09

Michael J. Barber


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' 
like image 24
jaypal singh Avatar answered Sep 16 '22 16:09

jaypal singh