What does \1 in sed do?

后端 未结 2 497
渐次进展
渐次进展 2020-12-23 20:52

I found this question really relevant to what I wanted: Parsing using awk or sed in Unix, however I can\'t figure out what the following does:

\'s/\\([,=]\\)         


        
相关标签:
2条回答
  • 2020-12-23 21:39

    Here's a simple example:

    $ echo 'abcabcabc' | sed 's/\(ab\)c/\1/'
    ababcabc
    $ echo 'abcabcabc' | sed 's/\(ab\)c/\1/g'
    ababab
    $ echo 'abcabcabc' | sed 's/\(ab\)\(c\)/\1d\2/g'
    abdcabdcabdc
    

    In the first command, only the first match is affected. In the second command, every match is affected. In both cases, the \1 refers to the characters captured by the escaped parentheses.

    In the third command, two capture groups are specified. They are referred to by using \1 and \2. Up to nine capture groups can be used.

    In addition to the g (global) operator (or without it, the first match), you can specify a particular match:

    $ echo 'aaaaaa' | sed 's/a/A/4'
    aaaAaa
    
    0 讨论(0)
  • 2020-12-23 21:49

    \(...\) would capture the characters specified inside of the parens and \1 would be used to reference the first match, this is a part of regex.

    0 讨论(0)
提交回复
热议问题