Filter a specific letter within a word using Grep

2019-09-20 00:26发布

问题:

I've been trying to find a way to filter a specific letter within a word using a regular expression. For exemple, filtering the letter "a" in the word "latin". Filtering only a letter would be simple using something like :

grep "\ba\b"

but I can't find a way to get the "a" only in a certain word.

Thanks for your help!

回答1:

You can pipe to another grep, like this:

grep "\ba\b" /path/to/input/file | grep -o "a"

The latter part of the pipe uses the o flag which only outputs the matched part. Alternatively grep -o "a" should return all a's.



标签: grep