






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Full Form grep stands for Global Regular Expression Print. fgrep stands for Fixed Grep. Purpose It is used to search for patterns or text in files. fixed or exact text Regular Expression It supports regular expressions. does not support Search Type It is useful for pattern- based and complex searches. simple and exact text searches Example grep "hello" file.txt fgrep "hello*" file.txt Speed It can be slower for some searches. It can be faster for fixed-string searches.
sed (Stream Editor) is a Linux command used to process text line by line. It is mainly used for searching, replacing, deleting, and inserting text in files.
sed can search for a particular pattern and print the lines containing that pattern.
sed -n '/pattern/p' filename
sed -n '/error/p' logfile.txt This prints only the lines containing the word error. Here, -n suppresses automatic printing and p prints the matched lines.
The s command is used for search and replace.
sed 's/pattern/replacement/' filename
sed 's/apple/mango/' fruits.txt This replaces the first occurrence of apple with mango in each line. To replace all occurrences in a line, use the g flag: sed 's/apple/mango/g' fruits.txt
“ A Regular Expression (Regex) is a pattern of characters used to match, search, and manipulate text .” It is commonly used in Linux commands such as grep, egrep, sed, and awk. Regex is mainly used for: Searching text in files. Filtering logs or command output. Validating patterns such as phone numbers and email addresses. Replacing text. There are two main types of Regular Expressions in Linux: BRE and ERE.
BRE stands for Basic Regular Expression. It is the default type used by grep and sed. It supports a basic set of regular expression operators. Advanced operators such as +, ?, |, and () generally require a backslash () to be used as special operators.
Operat or Meaning . Matches any single character ^ Matches the beginning of a line $ Matches the end of a line
Operat or Meaning occurrences [] Matches one character from a set [^] Matches a character not in the set \ Escape character
grep "^hello" file.txt This searches for lines starting with hello.
ERE stands for Extended Regular Expression. It is supported by egrep or grep -E. It includes the operators available in BRE. It provides additional operators for creating more complex patterns. Operators such as +, ?, |, and () can be used directly without escaping.
Operat or Meaning
The grep command is used in Linux to search for a specified pattern or text inside files. It displays the lines that match the given pattern. By default, grep uses Basic Regular Expressions (BRE) for pattern matching.
grep [options] pattern filename
Opti on Description Example -i Ignores uppercase and lowercase differences grep -i "linux" file.txt -n Shows the line number of matching lines grep -n "Linux" file.txt -v Displays lines that do not match the pattern grep -v "Linux" file.txt -c Counts the number of matching lines grep -c "Linux" file.txt -l Displays only the names of files containing the pattern grep -l "Linux" *.txt -w Searches for the exact word grep -w "Linux" file.txt -r Searches recursively inside directories grep -r "Linux" myfolder
1. Search for a word: grep "Linux" file.txt It displays all lines containing the word Linux.
2. Case-insensitive search: grep -i "linux" file.txt It matches Linux, linux, LINUX, etc. 3. Display line numbers: grep -n "Linux" file.txt It displays matching lines along with their line numbers. 4. Display non-matching lines: grep -v "Linux" file.txt It displays lines that do not contain Linux. 5. Count matching lines: grep -c "Linux" file.txt It displays the number of lines containing Linux.
5. Display line numbers: fgrep -n "Linux" file.txt It displays the matching lines along with their line numbers.