How to replace two or more spaces with one comma? | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


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 !!

How to replace two or more spaces with one comma?

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-14-2013
Scottie1954 Scottie1954 is offline
Registered User
 
Join Date: Jul 2011
Last Activity: 13 May 2014, 6:09 PM EDT
Posts: 43
Thanks: 10
Thanked 0 Times in 0 Posts
How to replace two or more spaces with one comma?

I'm using sh on hp-ux. I've got a file that looks like this.


Code:
-5.65   175
-16.17  160
-13.57  270
-51.72  260
-8.30   360
-42.71  460
-.38    375
-.20    375
-4.15   170
-21.53  560
-18.84  360

I'd like to replace all the whitespace between the columns with one comma. I can't seem to get the awk, sed, and tr examples I've found to handle the repeating spaces very well.

Thank you!
Sponsored Links
    #2  
Old 03-14-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 20 December 2014, 7:00 PM EST
Location: Galactic Empire
Posts: 3,395
Thanks: 240
Thanked 1,211 Times in 1,137 Posts

Code:
awk ' { print $1, $2 } ' OFS=, file

OR

Code:
awk ' { $1 = $1 } 1 ' OFS=, file

Sponsored Links
    #3  
Old 03-14-2013
Scottie1954 Scottie1954 is offline
Registered User
 
Join Date: Jul 2011
Last Activity: 13 May 2014, 6:09 PM EDT
Posts: 43
Thanks: 10
Thanked 0 Times in 0 Posts
Shoot, why didn't I think of that?! Worked like a magic spell. Looks like I need to learn more about the Output Field Separator. Thank you!
    #4  
Old 03-14-2013
ajayram_arya ajayram_arya is offline
Registered User
 
Join Date: Jan 2012
Last Activity: 17 December 2014, 10:42 AM EST
Posts: 158
Thanks: 34
Thanked 2 Times in 2 Posts
Try this


Code:
bash-3.00$ cat sample.txt|awk '{print $1"\t"$2'}|tr "\t" ","
-5.65,175
-16.17,160
-13.57,270
-51.72,260
-8.30,360
-42.71,460
-.38,375
-.20,375
-4.15,170
-21.53,560
-18.84,360


Last edited by Scrutinizer; 03-15-2013 at 01:08 AM.. Reason: code tags
Sponsored Links
    #5  
Old 03-14-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 21 December 2014, 2:24 PM EST
Location: Aachen, Germany
Posts: 4,889
Thanks: 80
Thanked 1,226 Times in 1,152 Posts

Code:
sed 's: \+:,:' file

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
Need help removing leading spaces from one field in comma seperated file Scottie1954 UNIX for Dummies Questions & Answers 5 06-15-2012 09:12 PM
Replace the | with Comma bharathappriyan Shell Programming and Scripting 6 11-10-2010 09:13 PM
How to Use Sed Command to replace white spaces with comma from between two fields - Mayank mayanksargoch Shell Programming and Scripting 8 07-07-2010 08:41 AM
Replace comma with newline eltinator Shell Programming and Scripting 1 02-11-2009 06:04 PM
Replace , (comma) with space mbarberis Shell Programming and Scripting 6 03-29-2005 10:35 AM



All times are GMT -4. The time now is 06:34 AM.