Insert linefeed in sed (Mac OS X)

前端 未结 9 960
长情又很酷
长情又很酷 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:52

    Unfortunately, for me, sed seems to ignore \ns in the replacement string.

    $ echo test1234foo123bar1234 | sed "s/\(1234\)/\n\1/g"
    testn1234foo123barn1234
    

    If that happens for you as well, an alternative is to use:

    $ echo test1234foo123bar1234 | sed "s/\(1234\)/\\`echo -e '\n\r'`\1/g"
    

    This should work anywhere and will produce:

    test
    1234foo123bar
    1234
    

    For your example with an input.txt file as input and output.txt as output, use:

    $ sed "s/\(1234\)/\\`echo -e '\n\r'`\1/g" input.txt > output.txt
    
    0 讨论(0)
  • 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'
    
    0 讨论(0)
  • 2020-11-29 01:56

    You may also use the $'string' feature of Bash:

    man bash | less -p "\\$'"
    
    printf  '%s' 'test1234foo123bar1234'  | sed $'s/\\(1234\\)/\\\n\\1/g'
    
    0 讨论(0)
  • 2020-11-29 01:58

    The newline in the middle of the command can feel a bit clumsy:

    $ echo abc | sed 's/b/\
    /'
    a
    c
    

    Here are two solutions to this problem which I think should be quite portable (should work for any POSIX-compliant sh, printf, and sed):

    Solution 1:

    Remember to escape any \ and % characters for printf here:

    $ echo abc | sed "$(printf 's/b/\\\n/')"
    a
    c
    

    To avoid the need for escaping \ and % characters for printf:

    $ echo abc | sed "$(printf '%s\n%s' 's/b/\' '/')"
    a
    c
    

    Solution 2:

    Make a variable containing a newline like this:

    newline="$(printf '\nx')"; newline="${newline%x}"
    

    Or like this:

    newline='
    '
    

    Then use it like this:

    $ echo abc | sed "s/b/\\${newline}/"
    a
    c
    
    0 讨论(0)
  • 2020-11-29 02:09

    Try this:

    $ echo test1234foo123bar1234 | sed "s/\(1234\)/\n\1/g"
    test
    1234foo123bar
    1234
    

    From Sed Gnu doc

    g
        Apply the replacement to all matches to the regexp, not just the first. 
    
    0 讨论(0)
  • 2020-11-29 02:11

    Your sed version apparently does not support \n in RHS (right-hand side of substitution). You should read THE SED FAQ maintained by Eric Pement to choose one of possible solutions. I suggest trying first inserting literal newline character.

    Below is the quote from it.


    4.1. How do I insert a newline into the RHS of a substitution?

    Several versions of sed permit \n to be typed directly into the RHS, which is then converted to a newline on output: ssed, gsed302a+, gsed103 (with the -x switch), sed15+, sedmod, and UnixDOS sed. The easiest solution is to use one of these versions.

    For other versions of sed, try one of the following:

    (a) If typing the sed script from a Bourne shell, use one backslash \ if the script uses 'single quotes' or two backslashes \\ if the script requires "double quotes". In the example below, note that the leading > on the 2nd line is generated by the shell to prompt the user for more input. The user types in slash, single-quote, and then ENTER to terminate the command:

     [sh-prompt]$ echo twolines | sed 's/two/& new\
     >/'
     two new
     lines
     [bash-prompt]$
    

    (b) Use a script file with one backslash \ in the script, immediately followed by a newline. This will embed a newline into the "replace" portion. Example:

     sed -f newline.sed files
    
     # newline.sed
     s/twolines/two new\
     lines/g
    

    Some versions of sed may not need the trailing backslash. If so, remove it.

    (c) Insert an unused character and pipe the output through tr:

     echo twolines | sed 's/two/& new=/' | tr "=" "\n"   # produces
     two new
     lines
    

    (d) Use the G command:

    G appends a newline, plus the contents of the hold space to the end of the pattern space. If the hold space is empty, a newline is appended anyway. The newline is stored in the pattern space as \n where it can be addressed by grouping \(...\) and moved in the RHS. Thus, to change the "twolines" example used earlier, the following script will work:

     sed '/twolines/{G;s/\(two\)\(lines\)\(\n\)/\1\3\2/;}'
    

    (e) Inserting full lines, not breaking lines up:

    If one is not changing lines but only inserting complete lines before or after a pattern, the procedure is much easier. Use the i (insert) or a (append) command, making the alterations by an external script. To insert This line is new BEFORE each line matching a regex:

     /RE/i This line is new               # HHsed, sedmod, gsed 3.02a
     /RE/{x;s/$/This line is new/;G;}     # other seds
    

    The two examples above are intended as "one-line" commands entered from the console. If using a sed script, i\ immediately followed by a literal newline will work on all versions of sed. Furthermore, the command s/$/This line is new/ will only work if the hold space is already empty (which it is by default).

    To append This line is new AFTER each line matching a regex:

     /RE/a This line is new               # HHsed, sedmod, gsed 3.02a
     /RE/{G;s/$/This line is new/;}       # other seds
    

    To append 2 blank lines after each line matching a regex:

     /RE/{G;G;}                    # assumes the hold space is empty
    

    To replace each line matching a regex with 5 blank lines:

     /RE/{s/.*//;G;G;G;G;}         # assumes the hold space is empty
    

    (f) Use the y/// command if possible:

    On some Unix versions of sed (not GNU sed!), though the s/// command won't accept \n in the RHS, the y/// command does. If your Unix sed supports it, a newline after aaa can be inserted this way (which is not portable to GNU sed or other seds):

     s/aaa/&~/; y/~/\n/;    # assuming no other '~' is on the line!
    
    0 讨论(0)
提交回复
热议问题