Need help removing leading spaces from one field in comma seperated file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Need help removing leading spaces from one field in comma seperated file
# 1  
Old 06-13-2012
Need help removing leading spaces from one field in comma seperated file

Using awk or sed, I'd like to remove leading spaces after a comma and before a right justified number in field 6. Sounds simple but I can't find a solution. Each field's formatting must stay intact.

Input:
Code:
40,123456-02,160,05/24/2012,02/13/1977, 10699.15,0

Output:
Code:
40,123456-02,160,05/24/2012,02/13/1977,10699.15,0

Any help is appreciated!

~Scottie

Last edited by joeyg; 06-13-2012 at 02:13 PM.. Reason: Please wrap data and sripts with CodeTags
# 2  
Old 06-13-2012
Your example only showed this occurring once; not sure if it occurs outside of that 6th column. But...

Code:
echo 40,123456-02,160,05/24/2012,02/13/1977, 10699.15,0 | sed 's/, /,/'
40,123456-02,160,05/24/2012,02/13/1977,10699.15,0

# 3  
Old 06-15-2012
My apologies. My original post was not very clear. I don't have any code yet for awk or sed, just a file with multiple lines of data that look like the example below (this is better than my original example):

Code:
 1,  3456-78, 42,05/02/2012,05/12/1984,  1230.00, 4

What I'd like to do is remove leading spaces from every field, if any, while retaining the existing formatting of each field's number. So after some scripting magic the above line should look like:

Code:
1,3456-78,42,05/02/2012,05/12/1984,1230.00,4

Thanks a bunch for your help!
# 4  
Old 06-15-2012
Hi Scottie1954,

Try the following, there are two spaces before the star;

Code:
cat "filename" | sed 's/,  */,/g' > "newfilename"

Regards

Dave
This User Gave Thanks to gull04 For This Post:
# 5  
Old 06-15-2012
That got me going the right direction, so thank you.

It took me two steps to get what I wanted:

Code:
sed 's/,  */,/g' FILE1 >junk1     # remove leading spaces before commas

Code:
sed 's/^ *//' junk1 >junk2        # remove leading space(s) from beginning of line

There's probably a more efficent way to combine the two but this got the job done.
# 6  
Old 06-15-2012
Hi Scottie1954,

Sorry I didn't look at the post properly, it would have worked with;

Code:
cat "filename" | sed 's/  */,/g' > "newfilename"

Where the command is the same as before, just minus the comma ","

Regards

Dave
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to parse comma separated field and removing comma in between number and double quotes

Hi Experts, Please support I have below data in file in comma seperated, but 4th column is containing comma in between numbers, bcz of which when i tried to parse the file the column 6th value(5049641141) is being removed from the file and value(222.82) in column 5 becoming value of column6. ... (3 Replies)
Discussion started by: as7951
3 Replies

2. Shell Programming and Scripting

Need Help in rearranging the content of a file comma seperated

I have a file with the below content a = test1 b = test2 a = test3 b= test4 c = test6 b = test5 d = test7 d = test9 Need the output to be as follows a = test1,test3 b = test2, test5 c = test6 d = test7, test9 (4 Replies)
Discussion started by: iron_michael86
4 Replies

3. Shell Programming and Scripting

Convert comma seperated file to line seperated.

Hi, I have data like this. 1,2,3,4 Output required: 1 2 3 4 I am trying to use tr function but getting error. Help is appreciated. (6 Replies)
Discussion started by: pinnacle
6 Replies

4. Shell Programming and Scripting

Removing leading spaces from the variable value.

Hi All, I am trying to replace the value of a xml tag with a new one. But, the existing value in the xml contain leading spaces and I tried to remove that with different sed commands but all in vain. For replacing the value I wrote the command in BOLD letters below: bash-3.00$... (3 Replies)
Discussion started by: khedu
3 Replies

5. Solaris

removing special characters, white spaces from a field in a file

what my code is doing, it is executing a sql file and the resullset of the query is getting stored in the text file in a fixed format. for that fixed format i have used the following code:: Code: awk -F":"... (2 Replies)
Discussion started by: priyanka3006
2 Replies

6. Shell Programming and Scripting

Trimming fields for comma or pipe seperated file

I have file like this FileA: abc , "helloworld" , america def,asia, japan ghi, africa, ipl Output Needed: abc,"helloworld",america def,asia,japan ghi,africa,ipl I would like to implement using awk. I want to trim each field for its leading and trailing spaces. (7 Replies)
Discussion started by: pinnacle
7 Replies

7. Shell Programming and Scripting

Removing blank lines from comma seperated and space seperated file.

Hi, I want to remove empty/blank lines from comma seperated and space seperated files Thanks all for help (11 Replies)
Discussion started by: pinnacle
11 Replies

8. Shell Programming and Scripting

Removing leading and trailing spaces only in PERL

Hi All, I have a file with the following contents with multiple lines 172445957| 000005911|8| 400 Peninsula Ave.#1551 | And,K |935172445957|000005911 607573888 |000098536 | 2|Ane, B |J |Ane |1868 |19861206|20090106|20071001 I want to trim the "leading and trailing spaces only" from... (2 Replies)
Discussion started by: kumar04
2 Replies

9. UNIX for Dummies Questions & Answers

Search and then concat 4m other file (comma seperated)

My query is now a bit simplified. file1.txt names; ID; value1 ; values N; ABC; 1 ; a18 ; ... CDF; 2 ; b16 ; .. ABC; 1 ; c13 ; ...... EFG; 3 ;d12 ; ... file2.txt ID(Unique);smVals; smVal1; smVal N; 1; ...; ...; ...; 2; ..; ..; ..; 3; ..; ..; ..; ... (1 Reply)
Discussion started by: szchmaltz
1 Replies

10. UNIX for Dummies Questions & Answers

Removing leading and trailing spaces of data between the tags in xml.

I am having xml document as below. <transactionid> 00 </transactionid> <tracknumber> 0 </tracknumber> <key> N/A </key> But the data contains leading and trailing spaces between the tags. Please let me know how can i remove these leading and trailing spaces between the tags.... (2 Replies)
Discussion started by: jhmr7
2 Replies
Login or Register to Ask a Question