Skip to content

merge tricks

Various tricks for merge two lines to one-side-by-side

Xargs

xargs -n2 -d'\n'

paste

paste -s -d ',\n' infile
paste -d " "  - - < filename

awk

awk 'NR%2{printf "%s ",$0;next;}1' yourFile
awk '{key=$0; getline; print key ", " $0;}'
awk 'ORS=NR%2?FS:RS' file
awk 'ORS=NR%2?FS:RS' file

```sh
awk '{ ORS = (NR%2 ? FS : RS) } 1' file
awk '{ ORS = (NR%2 ? "," : RS) } 1' file
awk '{ if (NR%2 != 0) line=$0; else {printf("%s %s\n", line, $0); line="";} } END {if (length(line)) print line;}'

sed

sed 'N;s/\n/ /' yourFile

while

while read line1; do read line2; echo "$line1, $line2"; done < data.txt

ex

ex -c "%g/KEY/j" -c "wq" data.txt

perl

perl -0pe 's/(.*)\n(.*)\n/$1 $2\n/g' 

Last update: June 18, 2022
Created: June 18, 2022