remove the spaces in between the fields [message #237387] |
Mon, 14 May 2007 05:57 |
piduruviswa
Messages: 12 Registered: May 2007 Location: Singapore
|
Junior Member |
|
|
Hello all,
I am trying to remove the spaces in between the fields and the file is a "|" delimeter one.
ex :
i/p is
xxx | 2007-02-28 | 2007-02-28 | | | 1
o/p should be
xxx|2007-02-28|2007-02-28|||1
I tried using gsub in awk but it didnt work..
awk -F "|" '{for (i=1;i<=NF; i++) gsub($i,/^[ \t]+|[ \t]+$/+" ",""); {print$0}}' xyz.dat
can some correct me if I am wrong some where??
|
|
|
|
Re: remove the spaces in between the fields [message #237444 is a reply to message #237405] |
Mon, 14 May 2007 09:10 |
piduruviswa
Messages: 12 Registered: May 2007 Location: Singapore
|
Junior Member |
|
|
Hi,
Thanks for your response.. but the problem is i have millions of records in the file and one more thing is there are lot of files like that. so it would be good if i have some command so that i can write a script...
Also there is address field where it should not trim that spaces in between....
ex
i/p
xxx | 2007-02-28 | 2007-02-28 | | | 1 | meyer singapore
xxx | 2007-02-28 | 2007-02-28 | | | 2 | meyer singapore
o/p
xxx|2007-02-28|2007-02-28|||1|meyer singapore
xxx|2007-02-28|2007-02-28|||2|meyer singapore
So only left and right spaces should be trimmed..
how can I achieve this??
rgds
vish
|
|
|
|
Re: remove the spaces in between the fields [message #237794 is a reply to message #237696] |
Tue, 15 May 2007 13:37 |
andrew again
Messages: 2577 Registered: March 2000
|
Senior Member |
|
|
Not quite the required solution, but good to remember...
dev>>cat orgfile
xxx | 2007-02-28 | 2007-02-28 | | | 1 | meyer singapore
xxx | 2007-02-28 | 2007-02-28 | | | 2 | meyer singapore
dev>>cat orgfile | tr -d " "
xxx|2007-02-28|2007-02-28|||1|meyersingapore
xxx|2007-02-28|2007-02-28|||2|meyersingapore
|
|
|
Re: remove the spaces in between the fields [message #238351 is a reply to message #237696] |
Thu, 17 May 2007 09:08 |
piduruviswa
Messages: 12 Registered: May 2007 Location: Singapore
|
Junior Member |
|
|
Hi
Thanks for that.. I did some modification for your command taking care of the first string and last string with spaces..
And the one which can be used to remove the spaces in left and right of the delimeter including first and last string with spaces is
sed 's/| */|/g;s/ *|/|/g;s/^[ \t]*//;s/[ /t]*$//' orgfile
Thanks a lot for your help
Tx
Vish
|
|
|