How do I pair every two lines of a text file with Bash? [duplicate] How do I pair every two lines of a text file with Bash? [duplicate] bash bash

How do I pair every two lines of a text file with Bash? [duplicate]


$ sed '$!N;s/\n/ /' infile192.168.1.1 hostname1192.168.1.2 hostname2192.168.1.3 hostname3


Here's a shell-only alternative:

while read first; do read second; echo "$first $second"; done


I love the simplicity of this solution

cat infile | paste -sd ' \n'192.168.1.1 hostname1192.168.1.2 hostname2192.168.1.3 hostname3

or make it comma separated instead of space separated

cat infile | paste -sd ',\n'

and if your input file had a third line like timestamp

192.168.1.1hostname114423289909192.168.1.2hostname214423289910192.168.1.3hostname314423289911

then the only change is to add another space in to the delimiter list

cat infile | paste -sd '  \n'192.168.1.1 hostname1 14423289909192.168.1.2 hostname2 14423289910192.168.1.3 hostname3 14423289911