Remove spaces before a delimiter

Hi All,

I need to modify a script to remove spaces from a csv file.
The csv file is delimited by the '~' character and I need to remove the spaces which appear before this character.
i.e
Sample input:
LQ001 SWAT 11767727 ~9104 ~001 ~NIRSWA TEST 18 ~2 ~Standard Test ~0011

Desired Output:
LQ001 SWAT 11767727~9104~001~NIRSWA TEST 18~2~Standard Test~0011

Am new to shell scripting and have been playing around with the sed
command but with limited success!!

Any help appreciated, thanks.

redoubtable@Tsunami ~ $ echo "LQ001 SWAT 11767727 ~9104 ~001 ~NIRSWA TEST 18 ~2 ~Standard Test ~0011"|sed -e 's/\s~/~/g'
LQ001 SWAT 11767727~9104~001~NIRSWA TEST 18~2~Standard Test~0011
redoubtable@Tsunami ~ $

Thanks for that.

The output I am getting though is

LQ001 SWAT 11767727 ~9104 ~001 ~NIRSWA TEST 18 ~2 ~Standard Test ~0011

??

Thanks
Sue

Try this:

echo 'LQ001 SWAT 11767727 ~9104 ~001 ~NIRSWA TEST 18 ~2 ~Standard Test ~001'|sed 's/ ~/~/g'

Thanks for that - Almost there but not quite!!

Unfortunately there is often more than one space before the '~' delimiter. Sorry, its not clear from my example above:

So

echo 'LQ001 SWAT 11767727 ~9104 ~001 ~NIRSWA TEST 18 ~2 ~Standard Test ~001'|sed 's/ ~/~/g'

LQ001 SWAT 11767727 ~9104~001~NIRSWA TEST 18 ~2~Standard Test~001

is only removing 1 space before the '~' rather than all spaces

Thanks
Sue

Hi Sue,

In this case this should solve all your problems:

sed 's/[<spc><tab>]*~/~/g'

Replace "<spc>" and "<tab>" with literal spaces and tab characters. It will remove any number of whitespace before a tilde ("~") character everywhere on the line.

I hope this helps.

bakunin