LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to remove excess whitespace from a document? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-remove-excess-whitespace-from-a-document-883794/)

LAPIII 05-31-2011 03:21 PM

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.

crts 05-31-2011 03:29 PM

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.

arizonagroovejet 05-31-2011 03:45 PM

Quote:

Originally Posted by Advice Pro (Post 4372293)
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.

catkin 06-01-2011 08:48 AM

Quote:

Originally Posted by arizonagroovejet (Post 4372317)
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

crts 06-01-2011 09:56 AM

Quote:

Originally Posted by catkin (Post 4373085)
Maybe this
Code:

$ dos2unix < input.txt | sed 's/[ \t]*$//' > output.txt

Or maybe even this:
Code:

sed 's/[ \t]*\r$//' input > output


All times are GMT -5. The time now is 04:46 PM.