Friday, January 18, 2008

HowTo: Squeezed Out Multiple Lines From Text File

If you have a text file with multiple lines running between each set of words or pharagraph, here's how to convert those multiple line breaks into single line breaks and totally remove paragraph lines, new lines or return lines inside text files.

Supposed you have a cluttered text file or configuration file with multiple line breaks or lines between set of words or paragraphs, you can easily remove them by using linux grep or tr command.

How to remove multiple empty lines, line breaks, new lines from a text file?

# cat testfile.txt
---------------------------------------------------------
1 1

ilovetux.com

2 2

3 3

4 4
---------------------------------------------------------

Blogspot may purged and blurred out the character space format from the above lines and remove new empty lines between the words, so here's a capture of the text file, see image shown below


To remove multiple lines or line breaks between each lines from text file using grep, simply

# cat testfile.txt | grep " "
# grep " " testfile.txt
Result:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 1
ilovetux.com
2 2
3 3
4 4
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Now to suppress multiple lines or line breaks between each lines from text file using tr, simply

# cat testfile.txt | tr -s "\n"
would give you the same result that grep command had produced

Result:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 1
ilovetux.com
2 2
3 3
4 4
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~




Enjoy.

Related Readings:
How To Delete and Remove New Lines from Text File
Strip and Remove Directory Name from Directory Path Name
Parse and Retrieve FileName Extension Only

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy