Insert linefeed in sed (Mac OS X)

前端 未结 9 959
长情又很酷
长情又很酷 2020-11-29 01:36

How do I insert a newline in the replacement part of sed?

This code isn\'t working:

sed \"s/\\(1234\\)/\\n\\1/g\" input.txt > output.txt
         


        
9条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 01:55

    The solaris version of sed I could convince to work this way (in bash):

    echo test1234foo123bar1234 | sed 's/\(1234\)/\
    \1/g'
    

    (you have to put the line break directly after the backslash).

    In csh I had to put one more backslash:

    echo test1234foo123bar1234 | sed 's/\(1234\)/\\
    \1/g'
    

    The Gnu version of sed simply worked using \n:

    echo test1234foo123bar1234 | sed 's/\(1234\)/\n\1/g'
    

提交回复
热议问题