0

I have a file that looks like this:

 2 1 word 78 3 other words 2 some other words here 54 bla bla 

I'd like to remove whitespaces and put a comma between values and rest. the output should look like

2,1 word 78,3 other words 2,some other words here 54,bla bla 

The command

sed -e 's/\s*([0-9])+\s.+/&/' 

does not change anything

3 Answers 3

5

This should do the trick?

sed -Ee 's/^[[:space:]]+([0-9]+)[[:space:]]+/\1,/' bob 2,1 word 78,3 other words 2,some other words here 54,bla bla 
Sign up to request clarification or add additional context in comments.

4 Comments

illegal option r (under OSX)
Well, clean up you tags, then :) ... you tagged it as Linux Bash Or install a decent sed using homebrew ;}
++; simply use -E instead of -r, and it'll work on OSX (incidentally, GNU sed supports -E too, even though the man page doesn't say so).
@mklement0 : thanks for the -E reference; I'll amend my answer.
1

This will work with any POSIX sed:

$ sed 's/^[[:space:]]*//; s/[[:space:]]*$//; s/[[:space:]]/,/' file 2,1 word 78,3 other words 2,some other words here 54,bla bla 

Comments

-2

Try:

sed -e 's/(\d) (\d)(*)/\1,\2\3' infile.txt 

2 Comments

sed: 1: "s/(\d) (\d)(*)/\1,\2\3 ": \1 not defined in the RE
@Arif: Did you actually try that ? :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.