Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I entered a list of number in Calc and saved it as CSV so that all the values on one line are separated by commas. I will describe what I'm seeking with bash in the following example:

INPUT

22,33
45,533

OUTPUT

22:33,45:533
share|improve this question
    
To better understand the question, add an example input and output, please. –  enzotib Jan 1 '12 at 14:52
    
@enzotib Done. And this time chose the correct CSV, so the input I described before was wrong. –  Oxwivi Jan 1 '12 at 15:05

4 Answers 4

up vote 3 down vote accepted

Not sure if I understand correctly, but for the input and output you shown, the following sed command can do the transformation, taking into account that your input file contains CR-LF line terminators:

sed 's/,/:/g;s/ /,/g' <<<$(tr -d '\r' <input_file) | tee output_file.tmp
mv output_file.tmp input_file
share|improve this answer
    
$ sed 's/,/:/g;s/ /,/g' <<<$(<z1.csv) [new line] ,50:239 –  Oxwivi Jan 1 '12 at 18:45
    
The terminal outputs only the last line of the CSV file which was 50,239. There's no new output file and the original file isn't changed either. –  Oxwivi Jan 2 '12 at 6:36
    
With the input file you gave in the question, my tests show that it works perfectly. It is probable the your real input file has some particularity not shown in the example input file, and I cannot say what without seeing it. Regarding the modification of the input file, you didn't specified your request in the question, it can be easily done with some additional instruction. –  enzotib Jan 2 '12 at 8:18
    
It doesn't matter how the result is visible to me, but even if the command worked properly, it did not show the complete results in the terminal. Here's the entirety of the file used. –  Oxwivi Jan 2 '12 at 9:48
    
@Oxwivi: see modified answer –  enzotib Jan 2 '12 at 9:56

Solution without external binaries - but a slower one (reads line by line and changes ',' → ':')

while read -u 3 -r line; do 
        echo -n "${line/,/:}," >> new_file
done 3< old_file
share|improve this answer

Inefficient, but sure to work:

Open up the file in gedit. Press Ctrl+H. In find, type , and in replace, type :. Then select Replace All.

Next, bring up the Find and Replace screen again. In the Find box, type \n and leave the Replace box empty. Then select Replace All.

That's it :)

share|improve this answer
1  
Thanks, I did not know how to express new lines (\n). And not quite inefficient if there's 'replace all' function. :) –  Oxwivi Jan 2 '12 at 11:32

This works:

cat test| sed s/,/:/g | sed s/$/,/g | xargs

But produces:

22:33, 45:533,

So you may have to use this longer version to get the exact result you asked for:

cat test| sed s/,/:/g | sed s/$/,/g | xargs | sed "s/, /,/g"

I am pretty sure there are better ways of doing this with sed/awk, though this does achieve what you ask for and is pretty easy to read.

share|improve this answer
    
$ cat z1.csv | sed s/,/:/g | sed s/$/,/g | xargs | sed "s/, /,/g" [new line] ,50:239 –  Oxwivi Jan 1 '12 at 18:47
    
The terminal outputs only the last line of the CSV file which was 50,239. There's no new output file and the original file isn't changed either. –  Oxwivi Jan 2 '12 at 6:37
    
So you want your modifications to happen in place, which you did not specify in the question (only "input" and "output"). –  totaam Jan 2 '12 at 8:40
    
Well, I don't mind if the original file was modified, but the output on the terminal only displayed the last line, not everything. –  Oxwivi Jan 2 '12 at 9:44
    
Then there's something strange with your initial data too. Probably needs crlf conversion. –  totaam Jan 2 '12 at 12:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.