whitespace problem

I have a single string as below:

Rat run after Cat[whitespace]

i.e. there is a single whitespace after Cat.

This causes my file to fail.

Is there a way I can remove any whitespace at the end of any string.

I tried sed 's/ *//g', but it removes all white space and the above string becomes like this RatrunafterCat. But i dont want the format to be changed. Plz help.
Thanks,

sed "s/ \s*$//"

" \s*" - replace one or more spaces
"$ " - at the end

Zedex,
First of all thanks,

is this command ok with one quotation mark and I put a g[global] .I also removed a space before \s from what u gave

sed 's/\s*$//g' x.csv>y.csv

It however didnt do the purpose. Am i wrong somewhere.

Thanks,

I think I interpreted my problem wrongly.

this white space is in a column middle of the file>
columna columnb columnc columnd
tom rom[whitespace] pom som
tok ghom poch[whitespace] zom

so i need to remove the whitespace from column b and columnc only and that too everytime at the end of the string.

Is there any way. Sorry abt that.

Thanks,

rubinpat

if columns are not separated by space then what is the separator ? if possible provide some actual data ..

Again sorry zedex, the fields are comma delimited

columna, columnb, columnc, columnd,
tom dom, rom pom[whitespace], pom pok, som pok
tok dok, ghom gok, poch gok[whitespace], zom zok

There is a single space or whitespace before the comma delimiters which is createing problems. I tried to see what it is through cat -v but was unable to get what it is. I removed it through vi editor but that's not the permanent solution. When I use sed command to remove whitespace it removes all the whitespaces there in thus making my file look like below but that space is not gone

so i need to remove the whitespace or the hidden space from column b and columnc only and that too everytime at the end of the string.

Honestly I hv no clue whats that

Is there any way.

Thanks,

> echo "tom dom, rom pom , pom pok, som pok"
tom dom, rom pom , pom pok, som pok
> echo "tom dom, rom pom , pom pok, som pok" | sed "s/ ,/,/"
tom dom, rom pom, pom pok, som pok

Really nice of you Joeyg, But I need a permanent solution or script to attach for this flat file whereby everytime we get it we can clean it up. Sorry to bother but do u have any clues.thanks

also I tried sed "s/ ,/,/" test.csv>tes1.csv

this one stays no clue why

i tried with echo too but the space stays ...........what is it god knows?

joeyg has provided the solution for your problem ...

if you are not sure what it is
you can open file in VIM then do ":set list" make sure you are not in insert mode.

if you are still not able to make out again in vim go to the place where there is something you dont want and type "ga" which will display hex/octal value for same . use perl command to remove this hex/octal value.

Maybe you have a tab character before a comma. Try one of these commands, to remove a space or a tab before a comma:

sed -e 's/ ,/,/g' -e 's/\t ,/,/g'

or

awk '{gsub("[ ,|\t ],", ",")}1'

Regards

i got it working