Header RSS Feed
If you only want to see the articles of a certain category, please click on the desired category below:
ALL Android Backup BSD Database Hacks Hardware Internet Linux Mail MySQL Monitoring Network Personal PHP Proxy Shell Solaris Unix Virtualization VMware Windows Wyse

How to remove a newline character of every second line in bash
Tuesday - Apr 1st 2014 - by - (0 comments)

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 (http://serverfault.com/questions/375096/bash-sed-awk-etc-remove-every-other-newline), 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.

Go to Homepage home
Linux Howtos how to's
Monitoring Plugins monitoring plugins
Links links

Valid HTML 4.01 Transitional
Valid CSS!
[Valid RSS]

6937 Days
until Death of Computers