sed use: expected context address

后端 未结 2 1898
情书的邮戳
情书的邮戳 2021-02-01 10:49

I use the sed command on macOS with the following text:

$ cat pets.txt 
This is my cat
  my cat\'s name is betty
This is your dog
  your dog\'s name         


        
2条回答
  •  忘掉有多难
    2021-02-01 11:09

    It works with GNU sed because you're taking advantage of a feature added to sed by the GNU project, which didn't previously exist in the program (and still doesn't in non-GNU versions).

    You can achieve the same results in non-GNU sed with something like this:

    sed -E '/dog/{N;N;N;s/(^|\n)/&# /g;}' pets.txt
    

    That is, once we see "dog", pull in the next three lines, too. Then stick a # + space after the beginning of the line (^) and all the newlines (\n). In order to be able to do that search and replace in a single regex, we need to enable extended regular expressions, which is what the -E does. Without it, we could do it with two s commands, one for the beginning of the line and one for the newlines:

    sed '/dog/{N;N;N;s/^/# /;s/\n/&# /g;}' pets.txt
    

    If you're looking for another way to do this on a stock Mac without installing GNU coreutils, you might want to reach for a different utility, for example awk:

    awk '/dog/ {l=3}; (l-- > 0) {$0="# "$0} 1' pets.txt
    

    or perl (same idea as the awk version, just different syntax):

    perl -pe '$l=3 if /dog/; s/^/# / if $l-- > 0;'  pets.txt  
    

提交回复
热议问题