How to use sed to extract substring

前端 未结 5 1702
执笔经年
执笔经年 2020-12-07 18:23

I have a file containing the following lines:

  
  &         


        
5条回答
  •  遥遥无期
    2020-12-07 19:14

    sed 's/[^"]*"\([^"]*\).*/\1/'

    does the job.

    explanation of the part inside ' '

    • s - tells sed to substitute
    • / - start of regex string to search for
    • [^"]* - any character that is not ", any number of times. (matching parameter name=)
    • " - just a ".
    • ([^"]*) - anything inside () will be saved for reference to use later. The \ are there so the brackets are not considered as characters to search for. [^"]* means the same as above. (matching RemoteHost for example)
    • .* - any character, any number of times. (matching " access="readWrite"> /parameter)
    • / - end of the search regex, and start of the substitute string.
    • \1 - reference to that string we found in the brackets above.
    • / end of the substitute string.

    basically s/search for this/replace with this/ but we're telling him to replace the whole line with just a piece of it we found earlier.

提交回复
热议问题