remove space in front or end of each field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting remove space in front or end of each field
# 1  
Old 03-22-2007
remove space in front or end of each field

Hi,

I have a txt file called a.txt which contain over 10,000 records and I would like to remove space before comma or after comma....like below:

The input (for example two record 00001,00002):
00001,client,card limited ,02292,N ,162:41 , 192, 94.589000, 0.000000, 94.589000, 0.000000,0 , 0, 0.000000, 0.000000, 0.000000, 0.000000,162:41 , 192, 94.589000, 0.000000, 94.589000, 0.000000,SIG ,SIN
00002,client2,hello Corporation Limited ,02507,N ,1594:23 , 193, 332.890000, 0.000000, 332.890000, 0.000000,0 , 0, 0.000000, 0.000000, 0.000000, 0.000000,1594:23 , 193, 332.890000, 0.000000, 332.890000, 0.000000,AUS ,AUD

output:
00001,client,card limited,02292,N,162:41,192,94.589000,0.000000,94.589000, 0.000000,0,0,0.000000,0.000000,0.000000,0.000000,162:41,192,94.589000, 0.000000,94.589000,0.000000,SIG,SIN
00002,client2,hello Corporation Limited,02507,N,1594:23,193,332.890000,0.000000,332.890000,0.000000,0 ,0,0.000000,0.000000,0.000000,0.000000,1594:23,193,332.890000,0.000000,332.890000,0.000000,AUS,AUD


condition:
- the 3nd field is a company name which may have space between each word. However, I just need to delete space at front and end split by ","
- the last field like record no#1 "SIN" may contain a few space after "SIN", I also need to delete it as well.

Please help and thanks!!!!!
# 2  
Old 03-22-2007
Hi,
You can achieve this using sed,
cat filename | sed 's/ ,/,/g' | sed 's/, /,/g' | sed 's/ $//g'

filename is the input file name.
Thanks
Raghuram
# 3  
Old 03-22-2007
Quote:
Originally Posted by Raghuram.P
Hi,
You can achieve this using sed,
cat filename | sed 's/ ,/,/g' | sed 's/, /,/g' | sed 's/ $//g'

filename is the input file name.
Thanks
Raghuram
Your statement is not work..nothing change. I think the sed is not in my system.
# 4  
Old 03-22-2007
To remove multiple leading/trailing spaces...
Code:
sed 's;^ *;;;s; *, *;,;g;s; *$;;' a.txt > b.txt

# 5  
Old 03-22-2007
Give a try on this..

awk -F"," '{ s=""; for(i=4;i<=NF;i++) { if (i==4) {s=$i; } else {s = s","$i;} } gsub(/ /,"",s); sub(/\(.*\) ,/",",$3); print $1","$2","$3","s; }' filename
# 6  
Old 03-22-2007
Quote:
Originally Posted by Ygor
To remove multiple leading/trailing spaces...
Code:
sed 's;^ *;;;s; *, *;,;g;s; *$;;' a.txt > b.txt

great! It's work..can you explain to me the statment? Thx!!
# 7  
Old 03-22-2007
Quote:
Originally Posted by happyv
great! It's work..can you explain to me the statment? Thx!!
it will become clearer like this:
Code:
sed 's/^ *//;s/ *, */,/g;s/ *$//' file

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Programming

Publish notification via application front end

hi All I use tomcat server to publish war file. How to send an notification to users via the application screen and it should dismiss once user clicks X mark. Any suggestions ? (0 Replies)
Discussion started by: anil529
0 Replies

2. Shell Programming and Scripting

How to remove alphabets/special characters/space in the 5th field of a tab delimited file?

Thank you for 4 looking this post. We have a tab delimited file where we are facing problem in a lot of funny character. I have tried using awk but failed that is not working. In the 5th field ID which is supposed to be a integer only of that file, we are getting corrupted data as below. I... (12 Replies)
Discussion started by: Srithar
12 Replies

3. Shell Programming and Scripting

Remove line starting from space till end.

Hi, I have a code tag, from which i have the below snippet: intelrpt.GetCMB_FB type=ODBC> intelrpt.GetCMB_FB type=SYBASE> I want the output like: intelrpt.GetCMB_FB intelrpt.GetCMB_FB That is remove the lines starting from WHITESPACE till end. Please help. I am new to... (7 Replies)
Discussion started by: anupdas
7 Replies

4. UNIX for Dummies Questions & Answers

Communicate to the OS(linux) using front end.

Hi guys , I want to develop a web page which is capable of executing the command on os and show the output on the browser.(Which involves reading and writing too.) I m using jsp language to develop the web page. How would i use it to communicate with my linux server? Any... (3 Replies)
Discussion started by: pinga123
3 Replies

5. UNIX for Dummies Questions & Answers

Sed $ appending to front, not to the end

I keep trying to append some astrix to the end of a line, but it keeps overwriting at the front of the line. These are the originals Fred Fardbarkle:674-843-1385:20 Parak Lane, Duluth, MN 23850:4/12/23:780900 Fred Fardbarkle:674-843-1385:20 Parak Lane, Duluth, MN 23850:4/12/23:780900 ... (5 Replies)
Discussion started by: DrSammyD
5 Replies

6. Shell Programming and Scripting

awk get rid of space in end of field

Hello. I'm using a file to "grep" in a 2nd one (with awk) cat file1 2 first user 9 second user 1 third user (with a space after user) I want to get the line except the 1st field so I do : field=$(gawk '{$1 =""; print $0}' file | sed 's/^ //') It works but it deletes... (5 Replies)
Discussion started by: xanthos
5 Replies

7. Programming

Running exe's from front end

Hi, I have created a tool which analyses and debugs cobol programs on Unix environment usin the C files. I now want to create a frontend for the tool in windows. For this i need to establish some kinda communication between the front the end and the back end. I know pipes in one way of... (0 Replies)
Discussion started by: Sinbad
0 Replies

8. UNIX for Advanced & Expert Users

Front end on Unix

Hi, I would like to develop a user interface on Solaris. Can anybody throw some light on currently available software utilities/ packages..? Thanks in Advance .. JS (4 Replies)
Discussion started by: shibz
4 Replies
Login or Register to Ask a Question