DEV Community

taijidude
taijidude

Posted on

grep makes you grin... ;-)

This week I did only a short post, I have a lot on my plate but wanted to keep the habit of writing These during my weekly train rides.

Anyway, I had to use grep today and realized that I had forgotten the parameters a bit. So here is a little refresher:

-v : Flips what the output grep will show you. If you search for lines that contain the word test just write:

grep "test"
Enter fullscreen mode Exit fullscreen mode

If you want all the lines that don't contain the word test just use the -v Parameter.

grep -v "test"
Enter fullscreen mode Exit fullscreen mode

-n : shows Line numbers

-r : searches the current directory and all subdirectories recursively

--include : useful if you want Limit your search to a certain file pattern or Type.

grep --include="pom.xml" "java.version"
Enter fullscreen mode Exit fullscreen mode

Or

grep --include="*.txt" "Test"
Enter fullscreen mode Exit fullscreen mode

Which searches in all txt files for lines containing the word test.

To save me some typing when need to search through my files for specific terms i have a function in my bash.rc:

function grin() {
 grep -r -n --include="$1" "$2"
}
Enter fullscreen mode Exit fullscreen mode

So when i want to search my Java Project for all XML files containing the term java.version i just type:

grin "*.xml" "java.version"
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
chrisgreening profile image
Chris Greening

lol I like that you called it grin

Collapse
 
taijidude profile image
taijidude

Beeing a bit silly helps, when the day gets annoying. ;-)