Triming spaces few columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Triming spaces few columns
# 8  
Old 12-29-2011
You can always post the exact requirement at the very first beginning.
This saves lots of time.

Try

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

# 9  
Old 12-29-2011
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...

Code:
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...

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

# 10  
Old 12-29-2011
One way using 'perl':
Code:
$ 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
# 11  
Old 12-29-2011
Thanks for quick reply. Is there any simple way using AWK command.
# 12  
Old 12-29-2011
Hi Bmk,

Try the following, it will work

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



Note: I have tried this in LINUX(RedhatE4).
# 13  
Old 12-29-2011
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
# 14  
Old 12-29-2011
Using awk:
Code:
$ 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

Last edited by birei; 12-29-2011 at 08:01 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - print columns with text and spaces

Hi, I'm using awk to print columns from a tab delimited text file: awk '{print " "$2" "$3" $6"}' file The problem I have is column 6 contains text with spaces etc which means awk only prints the first word. How can I tell awk to print the whole column content as column 6? Thanks, (10 Replies)
Discussion started by: keenboy100
10 Replies

2. Shell Programming and Scripting

How to remove spaces between the columns in UNIX script?.

Hi guru's, I am trying to write a script to generate a csv file by connecting to database run a query and put the values into csv file. But the problem i face is i am getting lot of space after one value.how can i remove those values?. Please help. #!/bin/bash export... (2 Replies)
Discussion started by: karingulanagara
2 Replies

3. Shell Programming and Scripting

Triming a string

Hi i have an input " load /appss/asdfas/... I want to take the string present between first / / i.e appss Input is "load /appss/asdfas/..." Expected output is appss Thanks in advance Ananth (9 Replies)
Discussion started by: Ananthdoss
9 Replies

4. Shell Programming and Scripting

print columns with spaces

Hi All I've a file which is similar to the one given below column1 coulmn2 column3 column4 A B C D X Y F G H I would like to get it in this format A|B|C|D ||X|Y F||G|H Is... (4 Replies)
Discussion started by: Celvin VK
4 Replies

5. Shell Programming and Scripting

Merge 2 columns/remove specific spaces

Hi, I have a requirement to remove certain spaces from a table of information, but I'm unsure where to start. A typical table will be like this: ABCDE 1 Elton John 25 12 15 9 3 ABCDE 2 Oasis 29 13 4 6 9 ABCDE 3 The Rolling Stones 55 19 3 8 6The goal is to remove only the spaces between... (11 Replies)
Discussion started by: danhodges99
11 Replies

6. Shell Programming and Scripting

convert rows (with spaces) to columns

Hey all, I have a list in the format ; variable length with spaces more variable information some more variable information and I would like to transform that 'column' into rows ; variable length with spaces more variable information some more variable information Any... (8 Replies)
Discussion started by: TAPE
8 Replies

7. Shell Programming and Scripting

Format data to columns addind spaces

Hi all, I have a problem to format data from different database queries into one look. The input data are as follows, every line has the same number of values but a different number of characters: adata, bdata, cdata, ddata fffdata, gdata, hdata, idata jdata, kdata, ... (6 Replies)
Discussion started by: old_mike
6 Replies

8. Shell Programming and Scripting

Triming spaces for any variable

Hi, I want to trim the spaces on left side of the variable value. For eg: if it is 4 spaces followed by value, All the spaces on the left should be supressed. Easy but want it quickly. Regards, Shiv@jad (6 Replies)
Discussion started by: Shiv@jad
6 Replies

9. Shell Programming and Scripting

Remove spaces from columns

I have a file with only one field something like this : 213.45 220.65 240.47 345.67 320.45 I want to remove all spaces in between. Is there any unix command for that ?Thanks in advance.. (2 Replies)
Discussion started by: jacks
2 Replies

10. Shell Programming and Scripting

removing spaces betweent columns

Hello Friends, Can any one help me with this issue: I would like to format a file: say if I have rows like: 4512 , SMITH , I-28984 ,, 4324 , 4343 42312 , SMITH , I-2EE8984 ,, 432E4E4 , 4343 I would like to have the output diaplayed like : 4512... (8 Replies)
Discussion started by: sbasetty
8 Replies
Login or Register to Ask a Question