Docsity

Ch:-4,Linux,Fifth Semester,3rd Year,Bca, Study Guides, Projects, Research of Linux skills

Toggle sidebar
bg1
1. What is difference between ‘Grep’ And
’Fgrep’ Command?
Point grep Command fgrep Command
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.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Ch:-4,Linux,Fifth Semester,3rd Year,Bca and more Study Guides, Projects, Research Linux skills in PDF only on Docsity!

1. What is difference between ‘Grep’ And

’Fgrep’ Command?

Point grep Command fgrep Command

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.

2. Explain search, replace, deletion and

insertion using sed.

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.

1. Search

 sed can search for a particular pattern and print the lines containing that pattern.

Syntax:

sed -n '/pattern/p' filename

Example:

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.

2. Replace

 The s command is used for search and replace.

Syntax:

sed 's/pattern/replacement/' filename

Example:

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

3. What is Regular Expression? Explain BRE

and ERE.

“ 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.

1. Basic Regular Expression (BRE)

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.

Common BRE operators:

Operat or Meaning . Matches any single character ^ Matches the beginning of a line $ Matches the end of a line

  • Matches zero or more

Operat or Meaning occurrences [] Matches one character from a set [^] Matches a character not in the set \ Escape character

Example:

grep "^hello" file.txt This searches for lines starting with hello.

2. Extended Regular Expression (ERE)

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.

Common ERE operators:

Operat or Meaning

  • One or more occurrences ? Zero or one occurrence () Groups expressions {m,n} Matches from m to n

4. Explain grep command with options and

examples.

 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.

Syntax

grep [options] pattern filename

Common grep Options

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

Examples

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.