grep stands for : g/re/p (globally search a regular expression and print)
SYNTAX = grep <SWITCH> pattern_list <FILE>
Below are the switches which we uses regularly to perform day to day activities.
i => Case Insensitive Search
w => Search for particular Word
r => Search in all the files under the current directory and its sub directory
v => Invert the Search.
c => Count, how many lines matches the given pattern/string
n => Shows line number while displaying the output.
Input File: F_Data_File.txt
EMPID|EMPNAME|EMPDEPT|EMPSAL|LOCATION
10001|A1|HR|10000|USA
10002|A2|FIN|20000|USA
10003|A3|NSS|30000|IND
10004|A4|SEC|40000|USA
10005|A5|TECH|50000|IND
10006|A6|TECH|60000|IND
10007|A7|TECH|70000|IND
Examples:
1. Search word TECH in file F_Input_Data.txt and list all the lines.
$ grep "tech" F_Input_Data.txt
Above will not return anything as grep is case sensitive.
$ grep "TECH" F_Input_Data.txt
Will return the below output:
10005|A5|TECH|50000|IND
10006|A6|TECH|60000|IND
10007|A7|TECH|70000|IND
if you want to achieve same output using first syntax, you need to add switch -i as given below:
$ grep -i "tech" F_Input_Data.txt
Switch i will force grep to do the search by ignoring the case. It will return the same output as above.
10005|A5|TECH|50000|IND
10006|A6|TECH|60000|IND
10007|A7|TECH|70000|IND
2. Search TEC in file F_Input_Data.txt and list all the lines.
$ grep -i "tec" F_Input_Data.txt
Above will return below output as all theses lines are having TEC
10005|A5|TECH|50000|IND
10006|A6|TECH|60000|IND
10007|A7|TECH|70000|IND
If you want to check whether word TEC is available in the file or not then you need to use -w switch.
$ grep -w "TEC" F_Input_Data.txt
It will not return anything as word TEC is not available in the file. hence if you are strictly looking for a word use -w switch.
3. print all the lines other than lines which are having TECH in it.
$ grep -v "TEC" F_Input_Data.txt
Will return the below output:
10001|A1|HR|10000|USA
10002|A2|FIN|20000|USA
10003|A3|NSS|30000|IND
10004|A4|SEC|40000|USA
Switch -v negate the search pattern, means it will return everything other than the pattern.
4. Search a pattern in all the files under the current directory and its sub directory
There is a directory "test" in current working directory. this directory also have the file F_Input_Data.txt.
$ grep -r "TECH"
Output: -r switch has searched the word TECH in all the files in current directory as well as in all the files in subdirectory.
F_Data_File.txt:10005|A5|TECH|50000|IND
F_Data_File.txt:10006|A6|TECH|60000|IND
F_Data_File.txt:10007|A7|TECH|70000|IND
test/F_Data_File.txt:10005|A5|TECH|50000|IND
test/F_Data_File.txt:10006|A6|TECH|60000|IND
test/F_Data_File.txt:10007|A7|TECH|70000|IND
5. Count the number of line contains the word TECH in it.
$ grep -c "TECH" F_Input_Data.txt
Output: 3
6. Print the line number in front of the line returned by grep as a result of search.
$ grep -n "TECH" F_Input_Data.txt
Output:
6:10005|A5|TECH|50000|IND
7:10006|A6|TECH|60000|IND
8:10007|A7|TECH|70000|IND
SYNTAX = grep <SWITCH> pattern_list <FILE>
Below are the switches which we uses regularly to perform day to day activities.
i => Case Insensitive Search
w => Search for particular Word
r => Search in all the files under the current directory and its sub directory
v => Invert the Search.
c => Count, how many lines matches the given pattern/string
n => Shows line number while displaying the output.
Input File: F_Data_File.txt
EMPID|EMPNAME|EMPDEPT|EMPSAL|LOCATION
10001|A1|HR|10000|USA
10002|A2|FIN|20000|USA
10003|A3|NSS|30000|IND
10004|A4|SEC|40000|USA
10005|A5|TECH|50000|IND
10006|A6|TECH|60000|IND
10007|A7|TECH|70000|IND
Examples:
1. Search word TECH in file F_Input_Data.txt and list all the lines.
$ grep "tech" F_Input_Data.txt
Above will not return anything as grep is case sensitive.
$ grep "TECH" F_Input_Data.txt
Will return the below output:
10005|A5|TECH|50000|IND
10006|A6|TECH|60000|IND
10007|A7|TECH|70000|IND
if you want to achieve same output using first syntax, you need to add switch -i as given below:
$ grep -i "tech" F_Input_Data.txt
Switch i will force grep to do the search by ignoring the case. It will return the same output as above.
10005|A5|TECH|50000|IND
10006|A6|TECH|60000|IND
10007|A7|TECH|70000|IND
2. Search TEC in file F_Input_Data.txt and list all the lines.
$ grep -i "tec" F_Input_Data.txt
Above will return below output as all theses lines are having TEC
10005|A5|TECH|50000|IND
10006|A6|TECH|60000|IND
10007|A7|TECH|70000|IND
If you want to check whether word TEC is available in the file or not then you need to use -w switch.
$ grep -w "TEC" F_Input_Data.txt
It will not return anything as word TEC is not available in the file. hence if you are strictly looking for a word use -w switch.
3. print all the lines other than lines which are having TECH in it.
$ grep -v "TEC" F_Input_Data.txt
Will return the below output:
10001|A1|HR|10000|USA
10002|A2|FIN|20000|USA
10003|A3|NSS|30000|IND
10004|A4|SEC|40000|USA
Switch -v negate the search pattern, means it will return everything other than the pattern.
4. Search a pattern in all the files under the current directory and its sub directory
There is a directory "test" in current working directory. this directory also have the file F_Input_Data.txt.
$ grep -r "TECH"
Output: -r switch has searched the word TECH in all the files in current directory as well as in all the files in subdirectory.
F_Data_File.txt:10005|A5|TECH|50000|IND
F_Data_File.txt:10006|A6|TECH|60000|IND
F_Data_File.txt:10007|A7|TECH|70000|IND
test/F_Data_File.txt:10005|A5|TECH|50000|IND
test/F_Data_File.txt:10006|A6|TECH|60000|IND
test/F_Data_File.txt:10007|A7|TECH|70000|IND
5. Count the number of line contains the word TECH in it.
$ grep -c "TECH" F_Input_Data.txt
Output: 3
6. Print the line number in front of the line returned by grep as a result of search.
$ grep -n "TECH" F_Input_Data.txt
Output:
6:10005|A5|TECH|50000|IND
7:10006|A6|TECH|60000|IND
8:10007|A7|TECH|70000|IND
No comments:
Post a Comment