Return only the regex match, not the whole line

后端 未结 1 1354
我寻月下人不归
我寻月下人不归 2021-01-01 15:03

I have a multiline document from which I\'m looking to extract a particular keyword and the word after that. It looks like this:



        
相关标签:
1条回答
  • 2021-01-01 15:29

    grep(1) has a -o flag that outputs only the matching part of the line. From the man page:

      -o, --only-matching
          Show only the part of a matching line that matches PATTERN.
    

    Your pattern isn't right to get the output you want, though. Try:

    $ egrep -o 'key \w+' file 
    key word1
    key word2
    
    0 讨论(0)
提交回复
热议问题