Git Bash - find words in a file (or string) that match a specified substring

cmd: cat test.txt | grep pin
results: prints all lines containing output

I only want grep now for words containing pin. What is the command for this?

Thank!

Everyone, thanks for your comments. I am using Git Bash (version 1.9.4). Grep does not have the -o option in this shell. There is a -w option. I tried: grep -w 'pin' test.txt but it returns nothing.

Does anyone use Git Bash to solve this problem?

Thanks everyone.

+3


source to share


3 answers


Assuming your file is named test.txt

, you can do:

grep -o '\S*pin\S*' test.txt



The flag -o

will only print matching words per line, as opposed to the entire line.

+1


source


You can use:



grep -o '[^[:blank:]]*pin[^[:blank:]]*' test.txt

      

+1


source


You can use the -w option.

$ cat test
pin
PIN
somepin
aping
spinx
$ grep pin test
pin
somepin
aping
spinx
$ grep -w pin test
pin
$

      

0


source







All Articles