remove extra spaces between fields

Hi,

I have a source file as mentioned below: I want to remove all the extra spaces between the fields.

a b--------|sa df-------|3232---|3
sf sa------|afs sdf-----|43-----|33
a b c------|adfsa dsf---|23-32|23

*Here '-' idicates spaces

Now, I want output as below:

a b|sa df|3232|3
sf sa|afs sdf|43|33
a b c|adfsa dsf|23-32|23

How can i achieve this?

Thanks,
Sri

sed 's/[[:space:]]\{2,\}\|//g' infile
a b|sa df|3232|3
sf sa|afs sdf|43|33
a b c|adfsa dsf|23-32|23

cat filename | tr -s ' '

This should work. Replaces two or more more spaces with one space.

Hi,

Thanks for your quick reply,

but im getting output as below using SED.

a bsa df32323
sf saafs sdf4333
a b cadfsa dsf23-3223

and using "tr" iam able to delete extra spaces but getting one space(as mentioned below) after each field, which i dont want :

a b |sa df |3232 |3
sf sa |afs sdf |43 |33
a b c |adfsa dsf |23-32 |23

please let me know where iam mistaken?

Thanks,
Sri

The sed works without problems on my box as you can see. Maybe you show what you issued and what the error output is or try it with gsed.

Using xpg4' sed on Solaris with "sed 's/[[:space:]]\{2,\}\|//g'" gives for me

a bsa df32323
sf saafs sdf4333
a b cadfsa dsf23 32|23

Must use "sed 's/[[:space:]]\{2,\}\|/|/g'" (just add | before /g) gives the good result

a b|sa df|3232|3
sf sa|afs sdf|43|33
a b c|adfsa dsf|23 32|23

Thanks all,

Goldorakk, your solution is working fine.

Can you please explain the code. I am unable to understand this.

Thanks,
Sri

sed -e 's/ *|/|/g' -e 's/| */|/g'  a.txt