Removing blank lines from text files automatically or interactively

Posted in Linux

Using sed’s delete, you can specify the start and beginning using the carot and dollar sign with nothing in between. You could replace this with something else using a search ’s/^$/stuff/g or just delete it like this:

sed ‘/^$/d’ dirtyblanklines.txt > freshnclean.txt

The only downside is if you want to move the filename back to the original, you can’t because you can’t do it all in one command or else you’ll wipe out your original file. You could type it all at once by chaining the two commands together with a semi-colon.

sed ‘/^$/d’ dirtyblanklines.txt > freshnclean.txt; mv freshnclean dirtyblanklines.txt

Interactively, in VI when you use regex to find and replace, you type :%s/^V^M//g (You should just see the ^M and in a different color if you’re using a modern linux’s vim with its vim-enhanced package.) You don’t actually type carot V carot M, but ctrl-v, ctrl-m as in “hold control, hit v, keep holding control, hit m, and let go of the keyboard”. What you’re really doing is escaping the ctrl-m. You can use the ctrl-v to escape any of the control characters, so if you see something funky like paragraph endings or smiley faces, try looking them up on a chart and using them right there in vim.

Posted by admica   @   10 February 2009
Tags : , , ,

Related Posts

Like this post? Share it!

Digg Twitter StumbleUpon Delicious Technorati Facebook RSS

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

Name

Email

Website

Previous Post
« Easy backup and restore of postgresql databases
Next Post
Has Stereoscopic 3-D Gaming Finally Arrived for the Average Consumer? »
Powered by Wordpress   |   Lunated designed by ZenVerse