removing blank newlines using sed.

Most people won’t find this paticularly useful, but as I’m prone to forget things:

sed -e '/^$/d'

will delete blank newlines from whatever it’s input is.

Tags: , ,
Categories: General Life Tags: , ,
  1. leE
    June 27th, 2008 at 13:32 | #1

    It’s actually faster to do this:

    tr -d ‘\n’

    Quick demo for you:

    $ time (cat *.txt | sed -e ‘/^$/d’ >> /dev/null)

    real 0m0.016s
    user 0m0.018s
    sys 0m0.001s

    $ time (cat *.txt | sed -e ‘/^$/d’ >> /dev/null)

    real 0m0.016s
    user 0m0.018s
    sys 0m0.001s

    $ time (cat *.txt | tr -d ‘\n’ >> /dev/null)

    real 0m0.010s
    user 0m0.011s
    sys 0m0.001s

    $ time (cat *.txt | tr -d ‘\n’ >> /dev/null)

    real 0m0.009s
    user 0m0.011s
    sys 0m0.001s

    It should also leave a smaller memory footprint:

    $ ls -lah `which tr`
    -r-xr-xr-x 1 root wheel 18K May 10 11:53 /usr/bin/tr
    $ ls -lah `which sed`
    -r-xr-xr-x 1 root wheel 33K May 10 11:53 /usr/bin/sed

    Every byte counts ;)

  1. No trackbacks yet.