首页 文章

在sed中用换行替换逗号

提问于
浏览
211

我有一个以逗号分隔的id文件 . 我正在尝试用新行代替逗号 . 我试过了:

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

但它不起作用 . 我错过了什么?

12 回答

  • 12

    请改用 tr

    tr , '\n' < file
    
  • 2

    使用 $'string'

    你需要一个反斜杠转义的文字换行符来获取sed . 至少在bash中, $'' 字符串将用真正的换行替换 \n ,但是你必须加倍sed将看到的反斜杠以逃避换行符,例如

    echo "a,b" | sed -e $'s/,/\\\n/g'
    
  • 0
    sed 's/,/\
    /g'
    

    适用于Mac OS X.

  • 4

    如果你的sed用法往往是完全替换表达式(就像我的那样),你也可以使用perl -pe

    $ echo 'foo,bar,baz' | perl -pe 's/,/,\n/g'
    foo,
    bar,
    baz
    
  • 5

    显然 \r 是关键!

    $ sed 's/, /\r/g' file3.txt > file4.txt
    

    转化为:

    ABFS, AIRM, AMED, BOSC, CALI, ECPG, FRGI, GERN, GTIV, HSON, IQNT, JRCC, LTRE,
    MACK, MIDD, NKTR, NPSP, PME, PTIX, REFR, RSOL, UBNT, UPI, YONG, ZEUS
    

    对此:

    ABFS
    AIRM
    AMED
    BOSC
    CALI
    ECPG
    FRGI
    GERN
    GTIV
    HSON
    IQNT
    JRCC
    LTRE
    MACK
    MIDD
    NKTR
    NPSP
    PME
    PTIX
    REFR
    RSOL
    UBNT
    UPI
    YONG
    ZEUS
    
  • 18

    这适用于MacOS Mountain Lion(10.8),Solaris 10(SunOS 5.10)和RHE Linux(Red Hat Enterprise Linux Server 5.3版,Tikanga)......

    $ sed 's/{pattern}/\^J/g' foo.txt > foo2.txt
    

    ...通过ctrl v j完成 ^J . 请记住 ^J 之前的 \ .

    PS,我知道RHEL中的sed是GNU,MacOS sed是基于FreeBSD的,虽然我不确定Solaris sed,但我相信这对任何sed都有用 . YMMV tho'......

  • 0

    为了完成它,这也有效:

    echo "a,b" | sed "s/,/\\$(echo -e '\n\r')/"
    
  • 5

    MacOS不同,有两种方法可以用mac中的sed来解决这个问题

    • 首先,使用 \'$'\n'' 替换 \n ,它可以在MacOS中工作:
    sed 's/,/\'$'\n''/g' file
    
    • 第二个,只需使用空行:
    sed 's/,/\
    /g' file
    
    • Ps . 注意 ' 分隔的范围
  • 277

    虽然我迟到了这篇文章,但只是更新我的发现 . 这个答案仅适用于 Mac OS X.

    $ sed 's/new/
    > /g' m1.json > m2.json
    sed: 1: "s/new/
    /g": unescaped newline inside substitute pattern
    

    在上面的命令中,我尝试使用Shift Enter添加新行,但不起作用 . 所以这次我尝试“逃避”错误告诉的“未转义的换行符” .

    $ sed 's/new/\
    > /g' m1.json > m2.json
    

    Worked! (in Mac OS X 10.9.3)

  • 249

    只是为了澄清:OSX上的sed的man-page(10.8; Darwin内核版本12.4.0)说:

    [...]

    Sed正则表达式

    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.
    

    [...]

    所以我猜一个人必须使用tr - 如上所述 - 或者漂亮

    sed "s/,/^M
    /g"
    

    注意:您必须在vi编辑器中键入<ctrl> -v,<return>才能获得'^M'

  • 104

    FWIW,以下行在Windows中工作,并用换行符替换路径变量中的分号 . 我正在使用我的git bin目录下安装的工具 .

    echo %path% | sed -e $'s/;/\\n/g' | less
    
  • 11

    我们也可以在sed中做到这一点 .

    你可以试试这个,

    sed -i~bak 's/\,/\n/g' file
    

    -i - 将更改文件中的修改 . 请谨慎使用此选项 .

    我希望这会对你有所帮助 .

相关问题