$ awk -F: '{a=a $1; b=b $2} END{print a FS b}' file
ABC DEFG MJk : 123 4587 36
We're seeing a lot of posts recently where people use tr '\n' ' '
or similar to convert newlines to something else. Except in rare situations don't do that as it converts a POSIX text file (that all POSIX text processing tools can read) into something else where YMMV. POSIX text lines end in \n
and a POSIX txt file is made up of POSIX text lines. If you use tr
or anything else to remove all newlines then what any subsequent POSIX text processing tool (awk, sed, etc., etc.) might do with that as input is undefined behavior.
Here's an example of some other behavior you might not expect but is actually defined by POSIX. Lets say we want to convert this multi-line string into a single space-separated line:
$ printf 'foo\nbar\n' | wc -l
2
using tr
to remove all \n
s:
$ printf 'foo\nbar\n' | tr '\n' ' '
foo bar $
$ printf 'foo\nbar\n' | tr '\n' ' ' | wc -l
0
vs a better way to do the same that outputs a POSIX text file and so gives a more intuitive result when piped to wc
:
$ printf 'foo\nbar\n' | paste -sd ' ' -
foo bar
$ printf 'foo\nbar\n' | paste -sd ' ' - | wc -l
1
ABC DEFG MJK : 123 4587 36
or wouldABC MJk DEFG : 36 123 4587
be OK?