Triming spaces few columns

File contains 10 columns,i want trim the 2,4,10 columns using single unix command.
Please suggest me how to do?

Thanks

Neither title nor the description clears your requirement (at least to me).
Please post sample data and required output.

How the columns are separated?

Excluding the header row,only data.

You need to specify how each column is separated. Is it space or comma or pipe?
Assuming default field separator:

awk '{$2=$4=$10=""; print}' input

Sample data file test1.txt

CCNI | data564_input1 | 264 | CCNI | data564_input1 |264
ccni| data564_input1    
CORO1A|155

i tried with above data not triming the spaces.

Hi bmk,

Your file seems irregular. All lines have different number of columns. What about trimming spaces from fourth field if only exists two?

Regards,
Birei.

Just i want trim the space specific columns,it not necessary to check the 4 th column is there or not.
Just trim....

You can always post the exact requirement at the very first beginning.
This saves lots of time.

Try

sed 's/[ ][ ]*//g' file

Just i used this command it's trimming all columns,i want trim specific column as like 3 or 8,not mandatory for the file columns...

sed 's/[ ][ ]*//g' test1.txt

---------- Post updated at 06:37 AM ---------- Previous update was at 06:26 AM ----------

Just i tried it's trimming all columns ,i want trim specefic column as like 2 and 4...

sed 's/[ ][ ]*//g' test1.txt

One way using 'perl':

$ cat infile
CCNI | data564_input1 | 264 | CCNI | data564_input1 |264
ccni| data564_input1    
CORO1A|155
$ cat script.pl
use warnings;
use strict;

while ( <> ) {
    chomp;
    my @f = split /\|/;
    for ( my $i = 0; $i <= $#f; $i++ ) {
                if ( $i == 1 || $i == 3 || $i == 9 ) {
                        $f[ $i ] =~ s/\A\s*//; 
                        $f[ $i ] =~ s/\s*\Z//;
                }
        }
        printf qq[%s\n], join qq[|], @f;
}
$ perl script.pl infile
CCNI |data564_input1| 264 |CCNI| data564_input1 |264
ccni|data564_input1
CORO1A|155

Regards,
Birei

Thanks for quick reply. Is there any simple way using AWK command.

Hi Bmk,

Try the following, it will work

tr -cd '[:alnum:],|,\012' < filename

Note: I have tried this in LINUX(RedhatE4).

Hey ,i know this command,it's trimming all columns. Check my privioues comments.

---------- Post updated at 06:53 AM ---------- Previous update was at 06:51 AM ----------

@sdebasis,it's not working

Using awk:

$ awk 'BEGIN { FS = OFS = "|" } { for ( i = 1; i <= NF; i++ ) { if ( i == 2 || i == 4 || i == 10 ) { sub( /^\s*/, "", $i ); sub( /\s*$/, "", $i ) } } { print } }' infile
CCNI |data564_input1| 264 |CCNI| data564_input1 |264
ccni|data564_input1
CORO1A|155

Regards,
Birei

ok, got it.

It will trim all the spaces between the columns.

i tried with same data and same command,not trimming the spaces. Help me

I followed the below post.

@Birei,it tried with your command,not trimming 2,4 and 10..
Help me.

Ok. Why don't we do things right, then?

1.- I put two scripts, one in 'perl' and other in 'awk'. Which of those don't work?
2.- I tested my own scripts and they seem to work with the data provided by you. Are you testing with similar data or not?
3.- Paste your input data you are testing, put the script you are running and the output it gives, or the error description.

I know a little about programming but can not read minds for the moment. Some users gave a solution and none of them seem to work, so think if we all are mistaken or perhaps you are not explaining your problem correctly , so try to help us a little bit, please. Thank you.

Regards,
Birei.

@Birei, Please below one... Perl script is working fine....

-bash-3.2$ awk 'BEGIN { FS = OFS = "|" } { for ( i = 1; i <= NF; i++ ) { if ( i == 2 || i == 4 || i == 10 ) { sub( /^\s*/, "", $i ); sub( /\s*$/, "", $i ) } } { print } }' test1.txt
CCNI | data564_input1 | 264 |CCNI| data564_input1 |264
ccni| data564_input1
CORO1A| 155