Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove tabs from blank lines using sed?

I'd like to use sed to remove tabs from otherwise blank lines. For example a line containing only \t\n should change to \n. What's the syntax for this?

like image 704
SundayMonday Avatar asked Sep 26 '11 17:09

SundayMonday


2 Answers

sed does not know about escape sequences like \t. So you will have to literally type a tab on your console:

sed 's/^    *$//g' <filename>

If you are on bash, then you can't type tab on the console. You will have to do ^V and then press tab. (Ctrl-V and then tab) to print a literal tab.

like image 142
Hari Menon Avatar answered Sep 26 '22 02:09

Hari Menon


To replace arbitrary whitespace lines with an empty line, use

sed -r 's/^\s+$//'

The -r flag says to use extended regular expressions, and the ^\s+$ pattern matches all lines with some whitespace but no other characters.

like image 40
Adam Mihalcin Avatar answered Sep 25 '22 02:09

Adam Mihalcin