Removing spaces at particular position

I have a file with delimiter ~

ABC~12~43~TR ~890~poi~YU ~56~65

What I want is to remove spaces from column 4,7 and other columns as it is
So, the final file becomes

ABC~12~43~TR~890~poi~YU~56~65

This may help you.

$ echo "ABC~12~43~TR ~890~poi~YU ~56~65" | sed 's/ //g'
ABC~12~43~TR~890~poi~YU~56~65

This will remove all the spaces
I am looking to remove spaces at particular position
Let me change the example
If I have

ABC~12~43~TR ~890~poi~YU ~56~65 ~BNP

I want is
ABC~12~43~TR~890~poi~YU~56~65~BNP

Then it's better to use awk

ok, how do I do it in awk?

This works in bash - is that what you were looking for?

line="ABC~12~43~TR ~890~poi~YU ~56~65 ~BNP"
IFS="~" read _1 _2 _3 _4 _5 _6 _7 _8 _9 _10 <<<"$line"
echo "${_1}~${_2}~${_3}~${_4/ /}~${_5}~${_6}~${_7/ /}~${_8}~${_9}~${_10}"

Only fields 4 and 7 remove the space.

If that's not it, can you restate what you're trying to do?

With awk :

awk -v FS=~ -v OFS=~ '
{
   gsub(/ /,"",$4);
   gsub(/ /,"",$5);
   gsub(/ /,"",$6);
   gsub(/ /,"",$7);
   gsub(/ /,"",$8);
   print;
} ' input_file > output_file

If you want to remove only trailing spaces, change the gsub statements like this :

sub(/ *$/,"",$4);

Jean-Pierre.