Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting spaces from last character to end of line 1

Status
Not open for further replies.

johngiggs

Technical User
Oct 30, 2002
492
US
Using sed, how would I go about deleting the spaces after the last letter or number in a line? I.E.

Sample Line spspspspspspsp

Where sp is a space on the line and should be replaced with:

Sample Line

I tried using:

sed -e 's/[a-z] *$//g;s/[0-9] *$//g' file

however it cuts off the last letter/number on the line.

Any help would be greatly appreciated.

Thanks,

John
 
sed -e 's! *$!!' file >file.new && mv file.new file
or
echo '1,$s! *$!!\nwq' | ex -s file


Hope This Help
PH.
 
Thanks, PHV!! That sed command works great!! Can you please explain the syntax of that command. I'm not sure why you used the !.

Thanks,

John
 
Code:
's! *$!!'
 ^^-^- ^
 || |  +- Replace by nothing, ie delete
 || +- Any number of space 'til end of line
 |+- Pattern delimiter, any char, not necessarly / 
 +- Substitute command
I don't use the / char as pattern delimiter coz I often use sed to modify pathname.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top