Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Grep Operator in Linux

Uploaded by

Aashish Hooda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Grep Operator in Linux

Uploaded by

Aashish Hooda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Grep Command

Grep command in Unix/Linux is the short form of ‘global


search for the regular expression’.
The grep command is a filter that is used to search for lines
matching a specified pattern and print the matching lines to
standard output.

The pattern is specified as a regular expression. A regular


expression is a string of characters that is used to specify a
pattern matching rule. Special characters are used to define the
matching rules and positions.
Anchor Characters: ‘^’ and ‘$’ at the beginning and end of the pattern are used to anchor the pattern to the start
of the line, and to the end of the line respectively.
Example: “^Name” matches all lines that start with the string “Name”. The strings “\<” and “\>” are used to
anchor the pattern to the start and end of a word respectively.

Wildcard Character: ‘.’ Is used to match any character.

Example:“^.$” will match all lines with any single character.

Escaped Characters: Any of the special characters can be matched as a regular character by escaping them with a
‘\’.
Example: “\$\*” will match the lines that contain the string “$*”
Character Range: A set of characters enclosed in a ‘[‘ and ‘]’ pair specify a range of characters to be matched.

“[aeiou]”

“[^xyz]” will match all lines that do not contain x, y or z.

“[0-9]”

Repetition Modifier: A ‘*’ after a character or group of characters is used to allow matching zero or more instances of
the preceding pattern.
The grep command supports a number of options for additional controls on the matching:
•-i: performs a case-insensitive search.
•-n: displays the lines containing the pattern along with the line numbers.
•-v: displays the lines not containing the specified pattern.
•-c: displays the count of the matching patterns.
EXAMPLE:

• Match all lines that start with ‘hello’. E.g: “hello there”
$ grep “^hello” file1

• Match all lines that end with ‘done’. E.g: “well done”
$ grep “done$” file1

• Match all lines that contain any of the letters ‘a’, ‘b’, ‘c’, ‘d’ or ‘e’.
$ grep “[a-e]” file1

• Match all lines that do not contain a vowel


$ grep “[^aeiou]” file1

• Match all lines that start with a digit following zero or more spaces. E.g: “ 1.” or “2.”
$ grep “ *[0-9]” file1

• Match all lines that contain the word hello in upper-case or lower-case
$ grep -i “hello”
Chown Command In Linux (File Ownership)

The chown command changes user ownership of a file, directory, or link in Linux. Every file is connected with an
owner user or group. It is vital to establish file and folder permissions appropriately.

SYNTAX: chown [OPTIONS] USER[:GROUP] FILE(s)

[OPTIONS] – the command can be used with or without


additional options.
[USER] – the username or the numeric user ID of the new
owner of a file.
[:] – use the colon when changing a group of a file.
[GROUP] — altering the group ownership of a file is optional.
FILE – the target file.
Relational Operators
Bourne Shell supports the following relational operators that are specific to numeric values. These operators do not work
for string values unless their value is numeric.
For example, following operators will work to check a relation between 10 and 20 as well as in between "10" and "20" but
not in between "ten" and "twenty".
Operator Description Example
-eq Checks if the value of two operands are equal or not; if yes, then [ $a -eq $b ] is not true.
the condition becomes true.

-ne Checks if the value of two operands are equal or not; if values are
not equal, then the condition becomes true. [ $a -ne $b ] is true.

-gt Checks if the value of left operand is greater than the value of right
operand; if yes, then the condition becomes true. [ $a -gt $b ] is not true.

-lt Checks if the value of left operand is less than the value of right
operand; if yes, then the condition becomes true. [ $a -lt $b ] is true.

-ge Checks if the value of left operand is greater than or equal to the
value of right operand; if yes, then the condition becomes true. [ $a -ge $b ] is not true.

-le Checks if the value of left operand is less than or equal to the
value of right operand; if yes, then the condition becomes true. [ $a -le $b ] is true.
It is very important to understand that all the conditional expressions should be placed inside square braces with
spaces around them. For example, [ $a <= $b ] is correct whereas, [$a <= $b] is incorrect.

NOTE:
The if...else statements
If else statements are useful decision-making statements which
can be used to select an option from a given set of options.
Unix Shell supports following forms of if…else statement −
•if...fi statement
•if...else...fi statement
•if...elif...else...fi statement
#!/bin/sh if...then...else...fi statement is a decision-making statement

a=10
if [ $a -lt $b ]
b=20 then
echo "$a -lt $b: a is less than b"
if [ $a -eq $b ] else
then echo "$a -lt $b: a is not less than b"
echo "$a -eq $b : a is equal to b"
else
echo "$a -eq $b: a is not equal to b“
if [ $a -ge $b ]
fi
then
echo "$a -ge $b: a is greater or equal to b"
if [ $a -ne $b ] else
then echo "$a -ge $b: a is not greater or equal to b"
echo "$a -ne $b: a is not equal to b"
else
echo "$a -ne $b : a is equal to b" if [ $a -le $b ]
then
if [ $a -gt $b ] echo "$a -le $b: a is less or equal to b"
then else
echo "$a -gt $b: a is greater than b" echo "$a -le $b: a is not less or equal to b"
else
echo "$a -gt $b: a is not greater than b"
There are various operators supported by each shell. We will discuss in detail about Bourne shell (default
shell) in this chapter.
We will now discuss the following operators −
•Arithmetic Operators
•Relational Operators
•Boolean Operators
•String Operators
•File Test Operators

#!/bin/sh val=`expr 2 + 2`
echo "Total value : $val"
-R makes every single file on the system under / (root) have rwxrwxrwx permissions.

You might also like