How to remove a newline character of every second line in bash

Written by - 0 comments

Published on - Listed in Linux Shell Unix Coding

Was looking for a solution to combine two lines to one but failed finding a solution with "tr" and "sed".

The output of the file looked like this and I wanted to combine the "alias" and "server_id" lines on one line:

cat serverlist | egrep "(alias|server_id)"
    "alias": "mymachine29",
    "server_id": 15,
    "alias": "mymachine30",
    "server_id": 12,
    "alias": "mymachine31",
    "server_id": 13,
    "alias": "mymachine32",
    "server_id": 10,
    "alias": "mymachine33",
    "server_id": 13,
    "alias": "mymachine34",
    "server_id": 10,
    "alias": "mymachine35",
    "server_id": 10,

By chance I came across this post (, which solves a similar request with the command "paste", I honestly haven't heard of before.

Let's try paste then!

cat serverlist | egrep "(alias|server_id)" | paste - -
   "alias": "mymachine29",          "server_id": 15,
    "alias": "mymachine30",          "server_id": 12,
    "alias": "mymachine31",          "server_id": 13,
    "alias": "mymachine32",          "server_id": 10,
    "alias": "mymachine33",          "server_id": 13,
    "alias": "mymachine34",          "server_id": 10,
    "alias": "mymachine35",          "server_id": 10,
    "alias": "mymachine36",          "server_id": 13,
    "alias": "mymachine37",          "server_id": 10,
    "alias": "mymachine38",          "server_id": 12,

Perfect! And much less complicated than trying to write a complex sed replacement...

Add a comment

Show form to leave a comment

Comments (newest first)

No comments yet.