Curly braces allow you to specify a set of characters from which the shell automatically forms all possible combinations
touch file{1,2,3} o/p: Will create 3 files file1,file2,file3
mkdir directory{1,2,3}{a,b,c} o/p: directories created: directory1a,directory1b,etc
touch file{a..z}
The following is the summary of various io-redirection and logical operators
Pattern searching using grep
Stands for globally search for the regular expression (RE) and print out the line.
Return status – success 0, pattern not found 1, file not found 2
ps -ef | grep root
The preceding command will show all processes running currently whose user ID is "root"
touch file{1,2,3} o/p: Will create 3 files file1,file2,file3
mkdir directory{1,2,3}{a,b,c} o/p: directories created: directory1a,directory1b,etc
touch file{a..z}
The following is the summary of various io-redirection and logical operators
Pattern searching using grep
Stands for globally search for the regular expression (RE) and print out the line.
Return status – success 0, pattern not found 1, file not found 2
ps -ef | grep root
The preceding command will show all processes running currently whose user ID is "root"
Metacharacter
|
Function
|
Example
|
Description
|
^
|
Beginning-of-line anchor
|
'^mango'
|
Will display all lines beginning with mango
|
$
|
End-of-line anchor
|
'mango'$'
|
Will display all lines ending with mango
|
.
|
Matches single character
|
'm..o'
|
Will display lines containing m, followed by two characters, followed by an o
|
*
|
Matches zero or more characters preceding the
asterisk
|
'*mango'
|
Will display lines with zero or more spaces, followed
by the pattern mango
|
[ ]
|
Matches single character in the set
|
'[Mm]ango'
|
Will display lines containing Mango or mango
|
[^]
|
Matches single character not in the set
|
'[^A–M] ango'
|
Will display lines not containing a character in the
range A through M,
followed by ango
|
\<
|
Beginning-of-word anchor
|
'\<mango'
|
Will display lines containing a word that begins with
mango
|
\>
|
End-of-word anchor
|
'mango\>'
|
Will display lines containing a word that ends with mango
|
We will try the following commands on the sample.txt file:
Sr. no.
|
Command
|
Description
|
1
|
grep Fruit sample.txt
|
This will show all lines
with pattern Fruit.
|
2
|
grep Fruit G*
|
This searches pattern Fruit in all files starting with G.
|
3
|
grep '^M' sample.txt
|
This searches all lines
starting with M.
|
4
|
grep '6$' sample.txt
|
This searches lines ending
with 6.
|
5
|
grep '1\..' sample.txt
|
This displays lines
containing 1 and any character after it.
|
6
|
grep '\.6' sample.txt
|
This shows lines containing
.6.
|
7
|
grep '^[AT]' sample.txt
|
This searches lines starting
with A or T.
|
8
|
grep '[^0-9]' sample.txt
|
This contains at least one
alphabet.
|
9
|
grep '[A-Z][A-Z] [A-Z]' sample.txt
|
This searches the upper
case, upper case space, and upper case word.
|
10
|
grep '[a-z]\{8\}' sample. txt
|
This displays all lines in
which there are at least eight consecutive lowercase letters.
|
11
|
grep '\<Fruit' sample.txt
|
This displays all lines
containing a word starting with Fruit. The \< is the beginning-of-word anchor.
|
12
|
grep '\<Fruit\>' sample. txt
|
This displays the line if it
contains the word Fruit.
The \< is the beginning-of-word
anchor and the \> is
the end-of-word anchor.
|
12
|
grep '\<[A-Z].*o\>' sample.txt
|
This displays all lines
containing a word starting with an uppercase letter, followed by any number
of characters and a word ending in o.
|
14
|
grep -n '^south' sample. txt
|
This displays line numbers
also.
|
15
|
grep –i 'pat' sample.txt
|
This displays case
insensitive search.
|
16
|
grep -v 'Onion' sample.txt > temp
mv temp sample.txt
|
This deletes the line
containing pattern.
|
17
|
grep –l 'Nuts' *
|
This lists files containing
pattern.
|
18
|
grep –c 'Nuts' sample.txt
|
This prints the number of
lines where pattern is present.
|
19
|
grep –w 'Nuts' sample.txt
|
This counts where the whole
world pattern is present, not a part of the word.
|
No comments:
Post a Comment