Operating System - HP-UX
1834568 Members
3941 Online
110069 Solutions
New Discussion

Deleting spaces at the end of line in a file

 
SOLVED
Go to solution
Sammy_2
Super Advisor

Deleting spaces at the end of line in a file

How do you delete blank spaces from the end of each line in a file ? (using either a awk, sed, or just vi editor)
good judgement comes from experience and experience comes from bad judgement.
9 REPLIES 9
Sanjay_6
Honored Contributor
Solution

Re: Deleting spaces at the end of line in a file

S.K. Chan
Honored Contributor

Re: Deleting spaces at the end of line in a file

This will delete trailing spaces and tabs from end of each line.

# cat file | sed 's/[ \t]*$//' > newfile
A. Clay Stephenson
Acclaimed Contributor

Re: Deleting spaces at the end of line in a file

This would be one method:

cat myfile | perl -e 'while () { $_ =~ s/\s+/$//; print "$_\n"; }' > newfile
If it ain't broke, I can fix that.
John Payne_2
Honored Contributor

Re: Deleting spaces at the end of line in a file

If you want to do it manually, vi the file. Go to the line you want and to the end of the line where the spaces are, then hit the 'x' key until all the spaces are gone. Then go to the next line you want to edit and continue.

To get out of vi, ':wq!'. If you want to get out without saving, ':q!'.

Hope it helps

John
Spoon!!!!
Sachin Patel
Honored Contributor

Re: Deleting spaces at the end of line in a file

Hi Sam you can use sed

# delete trailing whitespace (spaces, tabs) from end of each line
sed 's/[ \t]*$//' # see note on '\t' at end of file

# delete BOTH leading and trailing whitespace from each line
sed 's/^[ \t]*//;s/[ \t]*$//'

Sachin
Is photography a hobby or another way to spend $
Sammy_2
Super Advisor

Re: Deleting spaces at the end of line in a file

Thanks sanjay for the link.
Clay, glad you showed me a way in perl. Am learning perl and will try it.
Chan and Sachin, do appreciate it .
And John, thanks for the help but I am trying to do it globally.
good judgement comes from experience and experience comes from bad judgement.
H.Merijn Brand (procura
Honored Contributor

Re: Deleting spaces at the end of line in a file

Not for the points!

A.Clay, learn the command line switches:

cat myfile | perl -e 'while () { $_ =~ s/\s+/$//; print "$_\n"; }' > newfile

is equal to

perl -ple 's/\s+$//' newfile

easier to read, ain't it?
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor

Re: Deleting spaces at the end of line in a file

N/A

Hit submit toooo early

cat myfile | perl -e 'while () { $_ =~ s/\s+/$//; print "$_\n"; }' > newfile

s/\s+/$// is illegal syntax, you mean s/\s+$//

in substitutions without extra flags, the $ in perl will match *before* the newline, so above (and my previous post) will add newlines

perl -pe 's/\s+$//' outfile

should do. I now dropped the -l flag, which adds a newline to every print statement
Enjoy, Have FUN! H.Merijn
Graham Cameron_1
Honored Contributor

Re: Deleting spaces at the end of line in a file

This would seem to do it, with less hoeroglyphics than most so far...

cat myfile|sed "s/[[:space:]]*$//" > newfile

It also handles all whitespace characters.
BTW - is this an Oracle spool file? If so you can suppress the spaces using
"SET TRIMSPOOL ON".

Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.