LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-18-2009, 05:08 AM   #1
czezz
Member
 
Registered: Nov 2004
Location: Poland/Warsaw
Distribution: Slackware/Solaris
Posts: 554

Rep: Reputation: 30
how to clear file form spaces and add commas ?


How can I clear file form empty signs (spaces) and separate entries by comma "," ?

This is eg. file I got:

Code:
   47197.5514    164670.135 600.741702    .369141094
what I need to have:

Code:
47197.5514,164670.135,600.741702,.369141094
 
Old 06-18-2009, 05:09 AM   #2
kike_coello
Member
 
Registered: Jul 2005
Location: maryland
Distribution: Ubuntu 9.04
Posts: 88

Rep: Reputation: 17
use the tr command
 
Old 06-18-2009, 05:17 AM   #3
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 18 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290
Hi,

tr ' ' '.' < yourfile if only one space between characters.

If you have more than one space or 'spaces' is a tab then do a man tr on your linux to find the code for tab (I think it's \t).

Kind regards,

Eric
 
Old 06-18-2009, 05:27 AM   #4
kike_coello
Member
 
Registered: Jul 2005
Location: maryland
Distribution: Ubuntu 9.04
Posts: 88

Rep: Reputation: 17
sorry for not explaining with detail but i was busy at the moment, you can solve this with a single command like this:

1) if your text file has numbers separated by several spaces

"new.txt"
123 456 789

the first thing to do is make them be separated by one space like this:

cat new.txt | tr -s ' '

that command line sends the contents of the file new.txt into the command tr (piping) and tells tr to squeeze the spaces together. now you either redirect the output to a temporary file or you can redirect it to tr again like this:

cat new.txt | tr -s ' ' | tr ' ' ','

the second pipe sends the output of the squeezed together numbers and tells tr to change all the single spaces to commas.

i hope that helps

enrique
 
Old 06-18-2009, 05:30 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
show some of your code next time. you have been here from 2004, so i expect you to know how to do it by now.
Code:
awk '{$1=$1}1' OFS="," file
 
Old 06-18-2009, 05:35 AM   #6
kike_coello
Member
 
Registered: Jul 2005
Location: maryland
Distribution: Ubuntu 9.04
Posts: 88

Rep: Reputation: 17
ghostdog74, how do you post code?
 
Old 06-18-2009, 05:58 AM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Code:
sed -r 's/ +//;s/ +/,/g' file
 
Old 06-18-2009, 06:00 AM   #8
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Quote:
Originally Posted by kike_coello View Post
ghostdog74, how do you post code?
By embedding the text between CODE tags. In advanced mode, just select the text and click on #. In the quick reply box, you have to write them as the following:

[CODE]This is a line of code[/CODE]
 
Old 06-18-2009, 06:05 AM   #9
kike_coello
Member
 
Registered: Jul 2005
Location: maryland
Distribution: Ubuntu 9.04
Posts: 88

Rep: Reputation: 17
Code:
 this is a line of code
 
Old 06-18-2009, 06:05 AM   #10
kike_coello
Member
 
Registered: Jul 2005
Location: maryland
Distribution: Ubuntu 9.04
Posts: 88

Rep: Reputation: 17
thanks
 
Old 06-18-2009, 06:09 AM   #11
czezz
Member
 
Registered: Nov 2004
Location: Poland/Warsaw
Distribution: Slackware/Solaris
Posts: 554

Original Poster
Rep: Reputation: 30
kike_coello: many thanks - works excellent
ghostdog74: what code are you talking about ? I got only plain text file

colucix - nice sed but it wont work under Solaris. I will check it later under Linux (ghostdog74 - same about ur solution)

Thanks.

Last edited by czezz; 06-18-2009 at 06:10 AM.
 
Old 06-18-2009, 06:57 AM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
use nawk on solaris.

Code:
what code are you talking about ? I got only plain text file
I am talking about your effort! what did you try before you post in this forum.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing a comma separated CSV file where fields have commas in to trickyflash Linux - General 7 03-26-2009 03:30 PM
Bash script to remove capitalisation and spaces form a filename scuzzman Programming 11 05-18-2008 12:28 PM
Add spaces to a file using bash orfeo Linux - Newbie 5 05-28-2007 10:34 AM
I want Linux source code for FAT file system in user readable form not in binary form ramya272 Linux - Newbie 5 02-05-2004 07:54 PM
How to add Plugin in the form of "C" file ? suriyamohan Linux - Software 0 08-30-2003 01:52 AM


All times are GMT -5. The time now is 02:20 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration