LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-31-2011, 04:21 PM   #1
Advice Pro
Member
 
Registered: Mar 2009
Location: Virginia, US
Distribution: Ubuntu 10.10 & Debian 6.0.3,
Posts: 343

Rep: Reputation: 7
How to remove excess whitespace from a document?


I want to remove excess whitespace from the ends of lines in a document, but this code doesn't work:

Code:
$ cat input.txt | sed 's/[ \t]*$//' > output.txt
Nor does:
Code:
cat input.txt | sed 's/^[ \t]*//;s/[ \t]*$//' > output.txt
What am I doing wrong and are there other ways of automatically removing excess whitespace from the ends of lines.
 
Old 05-31-2011, 04:29 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,620

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Hi,

I ran your command and it worked fine. Not sure where the problem is. Are you, by any chance, trying to edit windows files?
If so then try the following:
Code:
sed 's/[ \t]*\r$//'
This will deal with the windows specific EOL character and transform it to a unix file. You could also run
Code:
dos2unix file
before you try your original 'sed' command again.
 
Old 05-31-2011, 04:45 PM   #3
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,093

Rep: Reputation: 197Reputation: 197
Quote:
Originally Posted by Advice Pro View Post
Code:
$ cat input.txt | sed 's/[ \t]*$//' > output.txt
You don't need the cat there
Code:
$ sed 's/[ \t]*$//' input.txt > output.txt

What crts means by 'windows files' is plain text files that were created in a Windows application and hence have Windows style line endings (CR followed by LF) rather than Unix style (LF only).

If you don't want to alter input.txt you can pipe it to dos2unix and pipe the output from dos2unix to sed.

Code:
$ cat input.txt | dos2unix | sed 's/[ \t]*$//' > output.txt
Feels like there should be a way to do that without that cat...

If you're still having trouble you could post input.txt as an attachment for people to experiment with.
 
Old 06-01-2011, 09:48 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by arizonagroovejet View Post
Code:
$ cat input.txt | dos2unix | sed 's/[ \t]*$//' > output.txt
Feels like there should be a way to do that without that cat...
Maybe this
Code:
$ dos2unix < input.txt | sed 's/[ \t]*$//' > output.txt
 
Old 06-01-2011, 10:56 AM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,620

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by catkin View Post
Maybe this
Code:
$ dos2unix < input.txt | sed 's/[ \t]*$//' > output.txt
Or maybe even this:
Code:
sed 's/[ \t]*\r$//' input > output
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep - remove lines which only contain whitespace. arizonagroovejet Linux - General 4 04-25-2009 10:41 AM
remove multiple files with whitespace coldy78 Linux - General 1 12-10-2007 03:23 AM
remove whitespace at end of file FunkyRes Programming 2 10-05-2004 01:31 AM
I want to remove excess fonts please point me in the direction Frybyte Linux - Newbie 2 02-16-2004 05:24 PM
Using sed in bash to remove whitespace jimieee Programming 3 01-28-2004 11:33 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration