Replace comma with newline in sed on MacOS?

后端 未结 13 2027
借酒劲吻你
借酒劲吻你 2020-11-27 09:27

I have a file of id\'s that are comma separated. I\'m trying to replace the commas with a new line. I\'ve tried:

sed \'s/,/\\n/g\' file

b

13条回答
  •  清酒与你
    2020-11-27 10:12

    Just to clearify: man-page of sed on OSX (10.8; Darwin Kernel Version 12.4.0) says:

    [...]

    Sed Regular Expressions

     The regular expressions used in sed, by default, are basic regular expressions (BREs, see re_format(7) for more information), but extended
     (modern) regular expressions can be used instead if the -E flag is given.  In addition, sed has the following two additions to regular
     expressions:
    
     1.   In a context address, any character other than a backslash (``\'') or newline character may be used to delimit the regular expression.
          Also, putting a backslash character before the delimiting character causes the character to be treated literally.  For example, in the
          context address \xabc\xdefx, the RE delimiter is an ``x'' and the second ``x'' stands for itself, so that the regular expression is
          ``abcxdef''.
    
     2.   The escape sequence \n matches a newline character embedded in the pattern space.  You cannot, however, use a literal newline charac-
          ter in an address or in the substitute command.
    

    [...]

    so I guess one have to use tr - as mentioned above - or the nifty

    sed "s/,/^M
    /g"
    

    note: you have to type <ctrl>-v,<return> to get '^M' in vi editor

提交回复
热议问题