ZoneO-tips for Mandriva Linux

Remove empty lines with sed

I have text files with tons of empty lines, how do I get rid of those in one second?

 
 

For html or php files that I'll post on the net, I like to get rid of empty lines. Usually, they are leftovers of search and replace and I have never been able to get any of the replace functions of the editors I use to get rid of them.

Now, sed is your friend... Written a long time ago for Unix systems, sed has been ported to a variety of operating systems, including Linux. It's is a non-interactive editor that works from a command line, with no GUI, so you do not waste time opening up windows, and clicking all over the place.

So, open up a konsole (Figure 1) and move into the directory where your file resides (cd MyDirectory). And here we go with the two lines that'll do the job

sed '/^$/d' myFile > tt
mv tt myFile
Start the Konsole
Figure 1: Start the Konsole

Here is what happens:

  • sed '/^$/d' myFile removes all empty lines from the file myFile and outputs the result in the console,
  • > tt redirects the output into a temporary file called tt,
  • mv tt myFile moves the temporary file tt to myFile.

Now, you may have 100 html files to correct at the same time. That's where foreach comes in... Let's say you want to correct all files ending with .html, here is what you should do:

  • open up a konsole (Figure 1),
  • move into the directory where your html files reside,
  • type the following commands:
    foreach file (*html)
    sed '/^$/d' $file > tt
    mv tt $file
    end
  • Finished!
12/2005
 

anonymous, 17 March 2006

Or even simpler:

foreach file(*html)

sed -i '/^$/d' $file

end

blueface, 13 April 2006

That does not work.

cubby, 05 October 2006

True, it would be:

foreach file (*html)

sed -i "" '/^$/d' $file

end

But you can also use:

sed -i "" '/^$/d' *.html

RF, 27 June 2007

cat filewith.txt | sed '/^$/d' >file without.txt

RICKLE, 21 November 2007

sed '/^s*$/' < input.txt > output.txt

This removes files with just white space on a line as well.

RICKLE, 21 November 2007

There is supposed to be a backslash before the s that didn't show up...

Spawn, 10 December 2007

This is crap. Do any of you have any idea how to write code on a website.

First error right off is:

bash: syntax error near unexpected token `('

Absolute crap. Doesn't work. Considering not every system will have foreach this is awfully stupid.

Gustavo, 11 December 2007

for i in *.html;do sed '/^$/d' < $i > tt;mv tt $i;done

Ismail, 20 December 2007

Sorry guys, but you make it so complicated with the scripting!

On the command line:

sed -i "" '/^$/d' *.html

Of course, I'd recommend backing up the whole directory as the "-i" option writes to the file "inplace".

Helldev, 18 January 2008

It will work...

grep -v '^$' 1.out > 4.out

Amit Pankaj, 27 May 2008

From all the above answers I would say the best is

sed -i "" '/^$/d' *.html

but i you need backup of the original then you should put the extension of the backup file e.g.

sed -i.bak "" '/^$/d' *.html

KDida, 13 August 2008

I have simillar problem but more complicated: I have 3 column but some of lines have only 2 column, for example :

xxx xxx xxx

xxx xxx xxx

xxx xxx xxx

xxx xxx <---- I want to remove this line

xxx xxx xxx

xxx xxx xxx

xxx xxx xxx

any idea ?

larry, 14 August 2008

awk -F' ' { if ( NF == 3 ) {print $0}}

larry, 14 August 2008

To print records only with 3 fields

awk -F' ' { if ( NF == 3 ) {print $0}} < source_file > new_file

mik, 10 December 2008

or simply:

awk 'NF==3' < source_file > new_file

mik, 10 December 2008

or simply:

awk 'NF==3' < source_file > new_file

des, 03 January 2009

Hey - before giving advice, try learning the commands!

Many of these are amateur advice with incorrect syntax.

Know it or shut up - bad advice is worse than no advice.

des, 03 January 2009

Hey - before giving advice, try learning the commands!

Many of these are amateur advice with incorrect syntax.

Know it or shut up - bad advice is worse than no advice.

Jason, 17 March 2009

Hey great tips!

This helped me a great deal.

Jason, 17 March 2009

Hey great tips!

This helped me a great deal.

anjan, 11 August 2009

pls send all unis programme

Tony, 28 August 2009

You can't rely on shell wildcards unless you're only dealing with a limited number of files with short names. As things scale, you may hit the dreaded "Arguments too long" failure. Better to use find in combination with xargs like:

find . -type f -name "*.html"| xargs -i $SHELL_COMMAND_YOU_WANT '{}'

The '{}' is substituted with each file name as it runs.

Shailen, 18 November 2009

Worked for me :-)

Shailen, 18 November 2009

Worked for me :-)

Shailen, 18 November 2009

Worked for me :-)

Kamaraj, 26 November 2009

Remove blank lines

This command also do the trick

grep . file_name > output_file_name

Kamaraj, 26 November 2009

Remove blank lines

This command also do the trick

grep . file_name > output_file_name

Ravi, 27 November 2009

awk '$0!~/[ t]/{print $0}' file_name

sriranga(77yrs old), 24 December 2009

tested for kannada scripts but have no effect.

sample kannada given below for further guidance.

please note that all kannada scripts furnished below will appear in unicode when previewed.

With regards,

-sriranga(77yrsold)

ಅಂ

ಅಃ

ಕಾ

ಕಿ

ಕೀ

ಕು

ಕೂ

ಕೃ

ಕೄ

ಕೆ

ಕೇ

ಕೈ

ಕೊ

ಕೋ

ಕೌ

ಕಂ

ಕಃ

ಗಾ

ಗಿ

ಗೀ

ಗು

ಗೂ

ಗೃ

ಗೄ

ಗೆ

ಗೇ

ಗೈ

ಗೊ

ಗೋ

ಗೌ

ಗಂ

ಗಃ

ಖಾ

ಖಿ

ಖೀ

sriranga(76yrsold), 24 December 2009

tested for kannada scripts but have no effect.

sample kannada given below for further guidance.

please note that all kannada scripts furnished below will appear in unicode when previewed.

With regards,

-sriranga(77yrsold)

ಅಂ

ಅಃ

ಕಾ

ಕಿ

ಕೀ

ಕು

ಕೂ

ಕೃ

ಕೄ

ಕೆ

ಕೇ

ಕೈ

ಕೊ

ಕೋ

ಕೌ

ಕಂ

ಕಃ

ಗಾ

ಗಿ

ಗೀ

ಗು

ಗೂ

ಗೃ

ಗೄ

ಗೆ

ಗೇ

ಗೈ

ಗೊ

ಗೋ

ಗೌ

ಗಂ

ಗಃ

ಖಾ

ಖಿ

ಖೀ

sriranga(76yrsold), 24 December 2009

tested for kannada scripts but have no effect.

sample kannada given below for further guidance.

please note that all kannada scripts furnished below will appear in unicode when previewed.

With regards,

-sriranga(77yrsold)

ಅಂ

ಅಃ

ಕಾ

ಕಿ

ಕೀ

ಕು

ಕೂ

ಕೃ

ಕೄ

ಕೆ

ಕೇ

ಕೈ

ಕೊ

ಕೋ

ಕೌ

ಕಂ

ಕಃ

ಗಾ

ಗಿ

ಗೀ

ಗು

ಗೂ

ಗೃ

ಗೄ

ಗೆ

ಗೇ

ಗೈ

ಗೊ

ಗೋ

ಗೌ

ಗಂ

ಗಃ

ಖಾ

ಖಿ

ಖೀ

sriranga(76yrs old), 25 December 2009

Remove blank lines (by Kamaraj)

This command also do the trick

grep . file_name > output_file_name

Above program solved my problem to remove blanklines(vertical - i,e spaces between two characters) for Kannada successfully

for which i am very much thankful to author Sri Kamaraj).

I want program to convert the horizongal text lines into vertical lines. for example: "testing" should be converted into vertical lines as shown below=

t

e

s

t

i

n

g

Szczad, 26 January 2010

@SRIRANGA:

That should do the thing:

COS="asdsad"; while [ -n "$COS" ]; do echo $COS|cut -b 1; COS=`echo $COS|cut -b 2- -`; done

Add a comment

Name:
Email or URL: (optional)
Security code
Code shown above:
 
 
© ZoneO-soft - Contact us - Start page