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: 27 July 2014, 11:40 PM EDT
Location: Galactic Empire
Posts: 3,356
Thanks: 230
Thanked 1,190 Times in 1,123 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: 22 July 2014, 12:17 PM EDT
Posts: 148
Thanks: 32
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 12: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: 28 July 2014, 7:42 AM EDT
Location: Aachen, Germany
Posts: 3,894
Thanks: 62
Thanked 925 Times in 878 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 08:12 PM
Replace the | with Comma bharathappriyan Shell Programming and Scripting 6 11-10-2010 08: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 07:41 AM
Replace comma with newline eltinator Shell Programming and Scripting 1 02-11-2009 05:04 PM
Replace , (comma) with space mbarberis Shell Programming and Scripting 6 03-29-2005 09:35 AM



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