Search code examples
bashsedtext-files

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


With a simple bash script I generate a text file with many lines like this:

192.168.1.1
hostname1
192.168.1.2
hostname2
192.168.1.3
hostname3

Now I want to reformat this file so it looks like this:

192.168.1.1 hostname1
192.168.1.2 hostname2
192.168.1.3 hostname3

How would I reformat it this way? Perhaps sed?


Solution

  • $ sed '$!N;s/\n/ /' infile
    192.168.1.1 hostname1
    192.168.1.2 hostname2
    192.168.1.3 hostname3