April 8

Linux: Using grep

Word count
grep -o ‘word’ filename | wc -l

One liner to find a running process and kill it
kill $(ps aux |grep -i ‘[/]usr/bin/puppet’ | awk ‘{print $2}’)

Find duplicate words in a file
grep -wo ‘[[:alnum:][:punct:]]+’ filename.txt | sort | uniq -cd

  • You may need to vary the “bracket expressions”, to fit the characters you are tryin go match.
    The above worked well when looking to find more than one server that is using the FQDN.

Search filesystem for a word:
find / -xdev -type f -print0 | xargs -0 grep -H “wordtosearchfor”


Copyright 2021. All rights reserved.

Posted April 8, 2019 by Timothy Conrad in category "Linux

About the Author

If I were to describe myself with one word it would be, creative. I am interested in almost everything which keeps me rather busy. Here you will find some of my technical musings. Securely email me using - PGP: 4CB8 91EB 0C0A A530 3BE9 6D76 B076 96F1 6135 0A1B