Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-13-2012
Registered User
 
Join Date: Jul 2011
Posts: 23
Thanks: 8
Thanked 0 Times in 0 Posts
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 01:13 PM.. Reason: Please wrap data and sripts with CodeTags
Sponsored Links
    #2  
Old 06-13-2012
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Location: Out running a Marathon.
Posts: 2,192
Thanks: 48
Thanked 126 Times in 118 Posts
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

Sponsored Links
    #3  
Old 06-15-2012
Registered User
 
Join Date: Jul 2011
Posts: 23
Thanks: 8
Thanked 0 Times in 0 Posts
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
Registered User
 
Join Date: Dec 2004
Location: Isle-of-Skye
Posts: 231
Thanks: 7
Thanked 26 Times in 26 Posts
Hi Scottie1954,

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


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

Regards

Dave
The Following User Says Thank You to gull04 For This Useful Post:
Scottie1954 (06-15-2012)
Sponsored Links
    #5  
Old 06-15-2012
Registered User
 
Join Date: Jul 2011
Posts: 23
Thanks: 8
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #6  
Old 06-15-2012
Registered User
 
Join Date: Dec 2004
Location: Isle-of-Skye
Posts: 231
Thanks: 7
Thanked 26 Times in 26 Posts
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
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Convert comma seperated file to line seperated. pinnacle Shell Programming and Scripting 6 12-07-2010 07:45 PM
Removing leading spaces from the variable value. khedu Shell Programming and Scripting 3 09-28-2009 03:26 PM
removing special characters, white spaces from a field in a file priyanka3006 Solaris 2 08-04-2009 09:30 AM
Removing blank lines from comma seperated and space seperated file. pinnacle Shell Programming and Scripting 11 05-08-2009 11:58 AM
Removing leading and trailing spaces only in PERL kumar04 Shell Programming and Scripting 2 04-04-2009 03:17 PM



All times are GMT -4. The time now is 12:08 AM.