Home » Infrastructure » Unix » remove the spaces in between the fields
remove the spaces in between the fields [message #237387] Mon, 14 May 2007 05:57 Go to next message
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 #237405 is a reply to message #237387] Mon, 14 May 2007 06:54 Go to previous messageGo to next message
tahpush
Messages: 961
Registered: August 2006
Location: Stockholm/Sweden
Senior Member

Will something like this do ?

[/SSW/oracle]vi orgfile

xxx | 2007-02-28 | 2007-02-28 | | | 1
xxx | 2007-02-28 | 2007-02-28 | | | 2

[/SSW/oracle]sed 's/ //g' orgfile > newfile

[/SSW/oracle]more newfile
xxx|2007-02-28|2007-02-28|||1
xxx|2007-02-28|2007-02-28|||21
Re: remove the spaces in between the fields [message #237444 is a reply to message #237405] Mon, 14 May 2007 09:10 Go to previous messageGo to next message
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 #237696 is a reply to message #237444] Tue, 15 May 2007 08:38 Go to previous messageGo to next message
tahpush
Messages: 961
Registered: August 2006
Location: Stockholm/Sweden
Senior Member

Will this do it for you ?

[/SSW/oracle]more orgfile
xxx | 2007-02-28 | 2007-02-28 | | | 1 | meyer singapore
xxx | 2007-02-28 | 2007-02-28 | | | 2 | meyer singapore

[/SSW/oracle]sed 's/| */|/g;s/ |/|/g' orgfile
xxx|2007-02-28|2007-02-28|||1|meyer singapore
xxx|2007-02-28|2007-02-28|||2|meyer singapore

Re: remove the spaces in between the fields [message #237794 is a reply to message #237696] Tue, 15 May 2007 13:37 Go to previous messageGo to next message
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 Go to previous message
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
Previous Topic: char. position in the text file.
Next Topic: interview questions on unix and shell scripting platform
Goto Forum:
  


Current Time: Fri Nov 22 03:57:12 CST 2024