Pascal
Pascal

Reputation: 41

Git Bash - find words in a file (or string) matching specified sub-string

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

I want to now only grep for words containing pin. What is the command to so that?

Thanks!

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

Anyone using Git Bash to solve this issue?

Thank you all.

Upvotes: 4

Views: 17415

Answers (3)

Sriharsha Kalluru
Sriharsha Kalluru

Reputation: 1823

You can use -w option.

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

Upvotes: 0

anubhava
anubhava

Reputation: 785256

You can use:

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

Upvotes: 2

celeritas
celeritas

Reputation: 2281

Assuming your file is called test.txt, you can do:

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

The -o flag will print only the matching words on the line, as opposed to the whole line.

Upvotes: 1

Related Questions