0

This question already has an answer here:

I have the following text:

serverName: NZRC222
total: 8.46 GB
serverId: 259695

serverName: NZRC333
total: 50.13 TB
serverId: 260582

serverName: NZRC555
total: 9.31 TB
serverId: 260956

My desired output would be:

serverName: NZRC222,
total: 8.46 GB,
serverId: 259695,

serverName: NZRC333,
total: 50.13 TB,
serverId: 260582,

serverName: NZRC555,
total: 9.31 TB,
serverId: 260956,

Basically, I want to generate a csv file which can be imported to the Excel.

When I go with : sed '/\w.*/ a ,' file I get this:

serverName: NZRC222
,
total: 50.13 TB
,
serverId: 260582
,

serverName: NZRC333
,
total: 9.31 TB
,
serverId: 260956
,

Any suggestions? Thank you in advance!

marked as duplicate by Stephen Kitt, Jeff Schaller, ilkkachu, roaima, thrig Apr 4 '17 at 22:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3

You can replace non-empty lines with the line and comma:

sed 's/.\+/&,/'

or

sed -E 's/.+/&,/'

& stands for the matched text, which is the whole line here.

  • Bingo, this also works :) sed 's/\w.*/&,/g' Didn't know for &.Thanks for quick answer! – fugitive Apr 4 '17 at 17:47
  • 1
    Why not just s/.$/&,/? why the g flag at all? – steeldriver Apr 4 '17 at 18:30
  • Thanks @steeldriver, I’ve dropped the anchors and the g (force of habit). Replacing only the last character is even better, but I’ll let you add that if you want... Sato’s answer is even better. – Stephen Kitt Apr 4 '17 at 18:43
2

With sed:

sed '/./ s/$/,/' file

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