ChatGPT解决这个技术问题 Extra ChatGPT

Can grep show only words that match search pattern?

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 can do:

grep "th" *

but the output will be something like (bold is by me);

some-text-file : the cat sat on the mat  
some-other-text-file : the quick brown fox  
yet-another-text-file : i hope this explains it thoroughly

What I want it to output, using the same search, is:

the
the
the
this
thoroughly

Is this possible using grep? Or using another combination of tools?

Is there a way one can print those matched words without changing the lines. Rather the matched string should remain in the same line?
tac file.log | grep "In msg::" | grep -oh "templateId=.*, temp"

S
Sergey Vyacheslavovich Brunov

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.

@user181548, The grep -o option works only for GNU grep. So if you are not using GNU grep, it might not work for you.
@A-B-B It depends if you want to display the name of the matched file or not. I'm not sure under what conditions it does and doesn't display, but I do know that when I used grep across a number of directories it did display the full file path for all matched files, whereas with -h it just displayed the matched words without any specification about which file it is. So, to match the original question, I think it is necessary in certain circumstances.
I needed an explanation for what "\w*th\w*" * means, so I figured I'd post. \w is [_[:alnum:]], so this matches basically any "word" that contains 'th' (since \w doesn't include space). The * after the quoted section is a glob for which files (i.e., matching all files in this directory)
\w is not generally portable to grep -E; for proper portability, use the POSIX character class name [[:alnum:]] instead (or [_[:alnum:]] if you really want the underscore, too; or try grep -P if your platform has that).
@A-B-B Given the desired output shown by the OP the -h is entirely necessary I would say.. ?
S
Stack Underflow

Cross distribution safe answer (including windows minGW?)

grep -h "[[:alpha:]]*th[[:alpha:]]*" 'filename' | tr ' ' '\n' | grep -h "[[:alpha:]]*th[[:alpha:]]*"

If you're using older versions of grep (like 2.4.2) which do not include the -o option, then use the above. Else use the simpler to maintain version below.

Linux cross distribution safe answer

grep -oh "[[:alpha:]]*th[[:alpha:]]*" 'filename'

To summarize: -oh outputs the regular expression matches to the file content (and not its filename), just like how you would expect a regular expression to work in vim/etc... What word or regular expression you would be searching for then, is up to you! As long as you remain with POSIX and not perl syntax (refer below)

More from the manual for grep

-o      Print each match, but only the match, not the entire line.
-h      Never print filename headers (i.e. filenames) with output lines.
-w      The expression is searched for as a word (as if surrounded by
         `[[:<:]]' and `[[:>:]]';

The reason why the original answer does not work for everyone

The usage of \w varies from platform to platform, as it's an extended "perl" syntax. As such, those grep installations that are limited to work with POSIX character classes use [[:alpha:]] and not its perl equivalent of \w. See the Wikipedia page on regular expression for more

Ultimately, the POSIX answer above will be a lot more reliable regardless of platform (being the original) for grep

As for support of grep without -o option, the first grep outputs the relevant lines, the tr splits the spaces to new lines, the final grep filters only for the respective lines.

(PS: I know most platforms by now would have been patched for \w.... but there are always those that lag behind)

Credit for the "-o" workaround from @AdamRosenfield answer


What about -o only working in GNU grep (as ksinkar mentioned in a comment on the accepted answer)?
@Brilliand hmm, im having trouble finding a linux implementation that does not support '-o', i can look for a work around if i know which platform to check against.
@pico The -o option is not present in the windows grep that installs with the git package (minGW?): "c:\Program Files (x86)\Git\bin\grep" --version grep (GNU grep) 2.4.2
@BrucePeterson i have added in AdamRosenfield workaround answer for -o : Help me check if the windows git includes tr / sed and its version. So i can check if this workaround works
@pico: for GIT: GNU sed version 4.2.1, tr (GNU textutils) 2.0
A
Abhinandan prasad

It's more simple than you think. Try this:

egrep -wo 'th.[a-z]*' filename.txt #### (Case Sensitive)

egrep -iwo 'th.[a-z]*' filename.txt  ### (Case Insensitive)

Where,

 egrep: Grep will work with extended regular expression.
 w    : Matches only word/words instead of substring.
 o    : Display only matched pattern instead of whole line.
 i    : If u want to ignore case sensitivity.

This doesn't seem to add anything over the existing answers from 4+ years before.
@tripleee I found my approach is better and simple so I posted this.
A
Adam Rosenfield

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

cat * | tr ' ' '\n' | grep th

no need cat. tr ' ' '\n' < file | grep th. Slow for big files.
This didn't work. The output still contained the filename and the entire line from the file that contained the match. Anyway, one of the other solutions offered worked. Thanks for the input though.
@ghostdog74: good point, although if you have more than file, you'll need to use cat. @Neil Baldwin: are you sure you typed it in right? When there's only one input file (stdin in this case), grep doesn't print the filename.
@Adam - yes, sorry Adam, it does work with one file but not multiple.
@ghostdog74 if the slow part is because of tr, he could do grep first, so tr would be applied only to matching lines: grep th filename | tr ' ' '\n' | grep th
f
fedorqui

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

g
gnat

grep command for only matching and perl

grep -o -P 'th.*? ' filename

What about display of only the matched group?
This doesn't work; it will only ever find th because you requested the shortest possible repetition of the wildcard.
@tripleee - it won't have that problem, because there's a space included at the end of the regex. However, it will miss words that don't have spaces after them, e.g. at the ends of lines.
B
Beau

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

f
fedorqui
cat *-text-file | grep -Eio "th[a-z]+"

or just grep -Eio "th[a-z]+" filename
Maybe see also Useless use of cat?
p
palswim

You can also try pcregrep. There is also a -w option in grep, but in some cases it doesn't work as expected.

From Wikipedia:

cat fruitlist.txt
apple
apples
pineapple
apple-
apple-fruit
fruit-apple

grep -w apple fruitlist.txt
apple
apple-
apple-fruit
fruit-apple

S
Sandeep

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

k
keebOo

I had a similar problem, looking for grep/pattern regex and the "matched pattern found" as output.

At the end I used egrep (same regex on grep -e or -G didn't give me the same result of egrep) with the option -o

so, I think that could be something similar to (I'm NOT a regex Master) :

egrep -o "the*|this{1}|thoroughly{1}" filename

The useless {1} quantifiers should be dropped. Or if you want to be consistent, t{1}h{1}e{1} etc.
can it print with the same line?
佚名

You could pipe your grep output into Perl like this:

grep "th" * | perl -n -e'while(/(\w*th\w*)/g) {print "$1\n"}'

that won't give the correct result. also, if using Perl, no need to use grep. do everything in Perl.
Thanks for pointing out the error, ghostdog74. I have changed it to print all the words on the line, not just the first.
like i said, grep is not necessary. perl -n -e'while(/(\s+th\w*)/g) {print "$1\n"}' file
up to you. i am just illustrating a point. If its not necessary, don't do it. that extra "|" will cost you one process more.
In Perl 5.10 or later: perl -nE '@a = /(regexp)/ig; say join "\n", @a'
p
pl1nk
$ 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.


That will still print the entire line containing the match. It constrains the actual match so that the no longer matches e.g. "these" or "bathe".
k
kenorb

ripgrep

Here are the example using ripgrep:

rg -o "(\w+)?th(\w+)?"

It'll match all words matching th.


I'm not sure why this answer is downvoted. I was using ripgrep looking for the answer to the same question and simply by adding the -o option I got exactly as desired.