November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

Categories

November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

Removing blank lines from a text file

Method 1: Using grep

$ grep -v '^$' infile.txt > outfile.txt

Method 2: Using sed

$ sed '/^$/d' infile.txt > outfile.txt

To remove blank lines from multiple files a script like the one below can be used

#!/bin/sh
files="/somefolder/*.txt"
for f in $files
do
  sed '/^$/d' $f > $f.tmp
  mv  $f.tmp $f
done

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>