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 (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.

RSS feed

Blog Tags:

  AWS   Android   Ansible   Apache   Apple   Atlassian   BSD   Backup   Bash   Bluecoat   CMS   Chef   Cloud   Coding   Consul   Containers   CouchDB   DB   DNS   Database   Databases   Docker   ELK   Elasticsearch   Filebeat   FreeBSD   Galera   Git   GlusterFS   Grafana   Graphics   HAProxy   HTML   Hacks   Hardware   Icinga   Influx   Internet   Java   KVM   Kibana   Kodi   Kubernetes   LVM   LXC   Linux   Logstash   Mac   Macintosh   Mail   MariaDB   Minio   MongoDB   Monitoring   Multimedia   MySQL   NFS   Nagios   Network   Nginx   OSSEC   OTRS   Office   PGSQL   PHP   Perl   Personal   PostgreSQL   Postgres   PowerDNS   Proxmox   Proxy   Python   Rancher   Rant   Redis   Roundcube   SSL   Samba   Seafile   Security   Shell   SmartOS   Solaris   Surveillance   Systemd   TLS   Tomcat   Ubuntu   Unix   VMWare   VMware   Varnish   Virtualization   Windows   Wireless   Wordpress   Wyse   ZFS   Zoneminder