Skip to content
← Back to Community
Unix S34: rep command and various options (-i,-c, -n, -w,-l,-v,-A,-B,-C)
Profile icon
sasivanga

Session-34. grep command and various options (-i,-c, -n, -w,-l,-v,-A,-B,-C)

grep===>ctrl+f ==>in windows

grep==> **Globally search a Regular Expression and Print it **
Global Regular Expression Print

grep ‘linux’ demo.txt
grep ‘linux’ demo.txt sub.txt
grep ‘linux’ *.txt
Simple grep command will search only in pwd only

**3) To Search data by ignoring case : **

By default grep command will consider case.If you want to ignore case then we should use -i option

grep -i 'sa' test.txt
empno,ename,sal
102,sai,3000

4) To display number of occurrences

We have use -c option
-c means count

grep -c 'sa' test.txt
2

**5) To display line numbers before results **

We have to use -n option

grep -n 'sa' test.txt
1:empno,ename,sal
3:102,sai,3000
grep -n 'sa' *.txt
file1.txt:1:sasi
file1.txt:2:sai
test.txt:1:empno,ename,sal
Test.txt:3:102,sai,3000

6) To display file names pattern exists :
-l to display

grep -l 'sa' *.txt
file1.txt
test.txt

**7) To print all lines except matched lines **

-v invert matched option

grep -v 'sai' test.txt

**8)To search for exact word in the file **

`grep -w ‘linux’ test.txt

grep -w 'linux' test.txt
linux is good
grep -i -w 'linux' test.txt
linux is good
Linux for better
LINUX is security
LINUX

cat abc.txt
10
10000
100000
10000000
101010101010
grep -w 10 abc.txt
10'

**9) Display before ,after and surroundings lines including search results **:
-A =====> means After
-B ===> Means Before
-C ====> Before and After

**9)We can use multiple commands in single **

grep -win 'linux' test.txt 8:linux is good 9:Linux for better 11:LINUX is security 12:LINUX grep -2 'sai' test.txt ==>by default it will take -c grep -A 'sai' test.txt grep -B 'sai' test.txt grep -C 'sai' test.txt
Voters
Profile icon
RaghuRam14
Profile icon
Highwayman
Profile icon
sasivanga