Can grep show only words that match search pattern?

前端 未结 14 1619
忘掉有多难
忘掉有多难 2020-11-22 14:35

Is there a way to make grep output \"words\" from files that match the search expression?

If I want to find all the instances of, say, \"th\" in a number of files, I

相关标签:
14条回答
  • 2020-11-22 15:18

    Just awk, no need combination of tools.

    # awk '{for(i=1;i<=NF;i++){if($i~/^th/){print $i}}}' file
    the
    the
    the
    this
    thoroughly
    
    0 讨论(0)
  • 2020-11-22 15:20

    I was unsatisfied with awk's hard to remember syntax but I liked the idea of using one utility to do this.

    It seems like ack (or ack-grep if you use Ubuntu) can do this easily:

    # ack-grep -ho "\bth.*?\b" *
    
    the
    the
    the
    this
    thoroughly
    

    If you omit the -h flag you get:

    # ack-grep -o "\bth.*?\b" *
    
    some-other-text-file
    1:the
    
    some-text-file
    1:the
    the
    
    yet-another-text-file
    1:this
    thoroughly
    

    As a bonus, you can use the --output flag to do this for more complex searches with just about the easiest syntax I've found:

    # echo "bug: 1, id: 5, time: 12/27/2010" > test-file
    # ack-grep -ho "bug: (\d*), id: (\d*), time: (.*)" --output '$1, $2, $3' test-file
    
    1, 5, 12/27/2010
    
    0 讨论(0)
  • 2020-11-22 15:26

    To search all the words with start with "icon-" the following command works perfect. I am using Ack here which is similar to grep but with better options and nice formatting.

    ack -oh --type=html "\w*icon-\w*" | sort | uniq
    
    0 讨论(0)
  • 2020-11-22 15:27

    You could translate spaces to newlines and then grep, e.g.:

    cat * | tr ' ' '\n' | grep th
    
    0 讨论(0)
  • 2020-11-22 15:28

    Try grep -o

    grep -oh "\w*th\w*" *
    

    Edit: matching from Phil's comment

    From the docs:

    -h, --no-filename
        Suppress the prefixing of file names on output. This is the default
        when there is only  one  file  (or only standard input) to search.
    -o, --only-matching
        Print  only  the matched (non-empty) parts of a matching line,
        with each such part on a separate output line.
    
    0 讨论(0)
  • 2020-11-22 15:29
    $ grep -w
    

    Excerpt from grep man page:

    -w: Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character.

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