Pattern matching and searching Flashcards
Which command enables users to search for strings or patterns within the content of a file?
grep
grep [-options] pattern filename
What does “grep -i” mean?
grep –i hall /student_files/day1/grepFile
grep –i: ignore case
Both upper or lower case is fine.
What does “grep -n” mean?
grep –n hall /student_files/day1/grepFile
grep –n: gives you the line number
What does “grep -v” mean?
grep –v hall /student_files/day1/grepFile
grep –v: returns everything but what the matcher
What does “grep -w” mean?
grep –w hall /student_files/day1/grepFile
grep –w: match the whole word
What does “grep -c” mean?
grep –c hall /student_files/day1/grepFile
grep –c: count the number of lines matching
What are Literal and meta character(s)?
Literal: “coffee”
Metacharacter: [ ] ! * . ? $ \ { } ( )
Escape sequence: \
Metacharacters
^: Match at start of line
$: Match at the end of line
. : Match a single character
*: Match zero or more occurrences of the preceding character
.*: Match with any number of characters
: Escape the metacharacter and treat them as a literal
What does the following command mean?
grep ‘^[0-9]’ /student_files/day1/grepFile
grep ‘^[0-9]’ /student_files/day1/grepFile
^: Match at start of line
[ ]: Match anything in the [ ] for 1 character position
So it means: Within the grepFile, find any line that starts with 0-9.
What does the following command mean?
grep ‘6.’ /student_files/day1/grepFile
grep ‘6.’ /student_files/day1/grepFile
. : Match a single character
What does the following command mean?
grep ‘6.’ /student_files/day1/grepFile
grep ‘6.’ /student_files/day1/grepFile
: Escape the metacharacter and treat them as a literal
What does the following command mean?
grep ‘192.*0’ /student_files/day1/grepFile
grep ‘192.*0’ /student_files/day1/grepFile
.*: Match with any number of characters
What does the following command mean?
grep ‘192*0’ /student_files/day1/grepFile
grep ‘192*0’ /student_files/day1/grepFile
*: Match zero or more occurrences of the preceding character
What does the “find” command do?
find [where to search] [what to search for] [action]
The find command enable users to locate files in the File System.
find searches through the directory tree recursively, locates the files and applies an optional action to the files found