Removing spaces from data

Hi,

I want to remove spaces from data.

Data is:

1,aa ,21, 22
2,a a ,23 ,24
3, ,25 ,26

output should be:

1,aa,21,22
2,a a,23,24
3, ,25,26

i.e

i have to remove leading and trailing spaces.Not the space between data and also i dont want to remove the space if data is not present for that field,i.e I have to keep that space as it; like it is present for record 3.

Please help me through this.

Thanks

echo '1,aa ,21, 22' | sed 's/ //g'

1,aa,21,22

this does it, although it can probably be simplified

> cat file37
1,aa ,21, 22
2,a a ,23 ,24
3, ,25 ,26
> sed "s/, ,/,~,/g" <file37 | sed "s/ ,/,/g" | sed "s/, /,/g" | sed "s/~/ /g"
1,aa,21,22
2,a a,23,24
3, ,25,26

Thanks for your help :slight_smile:

Or:

sed 's/\([^,]\) *,/\1,/g;s/, *\([^,]\)/,\1/g' infile