Bob
Bob

Reputation: 879

Using sed to remove leading and trailing white spaces

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

Upvotes: 0

Views: 3634

Answers (3)

tink
tink

Reputation: 15248

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

Upvotes: 5

Ed Morton
Ed Morton

Reputation: 204731

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

Upvotes: 1

Arif Burhan
Arif Burhan

Reputation: 505

Try:

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

Upvotes: -2

Related Questions