Chapter 3 - grep Flashcards
What does grep stand for?
Global regular expression print
What is grep used for?
grep searches text or files for a given string or pattern of text.
Explain grep option -w and how would you type it out?
Searches only for the exact word.
grep -w “the” resume.txt
(so it Won’t bring up words there or these)
Explain these grep options -l -c
- l shows just a list of the text file with matches. ( no lines of text)
- c similar to the -l option but adds a number with how many matches found in the file. (ex. blah.txt :7 )
What option in grep would you use to search subdirectories as well?
How would you type this command out?
-r the recursive option also searches subdirectories.
grep -winr “dairy” .
How do you use grep to search every file in the current working directory?
grep -win “john” ./*
you can also specify to search all .txt files in the directory.
grep -win “john” ./*.txt
Keep in mind you can join options together.
grep -win “test” poems.txt
Explain the grep -n option.
grep -n “test” poems.txt
Adds the number of the line to the out put.
Keep in mind you can join options together.
grep -win “test” poems.txt
Explain the grep -i option.
grep -i Makes the search Non case sensitive.
Explain the grep -v option.
grep -v Omits all the lines with that search ( grep -v “blah” poem.txt )
Explain these grep options
-B 4 -A 5 -C 2 And type out the command.
- B 4 Shows the first 4 lines of text Before our search match.
- A 5 Shows the last 5 lines of text After our search match.
- C 2 Shows the first 2 and last 2 line of our search option.
grep -win -A 5 “john” poems.txt
If cat is hung (flashing cursor & just repeating lines) how do you exit?
ctrl and D
We can also create a basic text file by
cat lazy dog.txt the lazy dog blah blah ctrl D (to exit)