Wednesday, 5 July 2017

grep command examples in Linux

grep command :- grep command is used to search files and print lines that have the matched string .
 
1) Print all lines that have the "error" word in file
grep error file.txt

2) Print all lines of files that have the "error" word
grep error file1.txt file2.txt file3.txt

3) Print all lines that have unix word in any capital and small letter
grep -i unix file.txt

Note:- "i" option for case insensitive search.

4) Print all lines those starts with digit
grep "^[0-9]*" file.txt

Note:-"^" denote start of line

5) Print all lines those does not start with digit
grep "^[^0-9]*" file.txt

Note:-"^" sign inside [ ] denote negation of given range

6) Print all lines those end with digit
grep "*[0-9]$" file.txt






7) Print all lines those does not end with digit
grep "*[^0-9]$" file.txt

8) Print all matched lines with 2 lines before matched line that have the "error" word in file
grep -B 2 error file.txt

9) Print all matched lines with 3 lines after matched line that have the "error" word in file
grep -B 2 error file.txt

10) Print all lines that does not contain "error" word in file
grep -v error file.txt

Note:- "v" option invert the selection

11) Print all non-empty lines
grep -v "^$" file.txt

12) Print line count that have matched pattern in file
grep -c error file.txt

13) Print all matched line with line number
grep -n error file.txt

14) Print the file name that contain matched pattern
grep -l error *.txt


Related Topics :-
touch command examples 

1 comment:

  1. Thanks for the given information you are providing content very usefull to users.
    Learn How to Work With Ref Cursor in ORACLE 12C For Beginner Actual cost of this course is 9,999/- But now Kuncham Software Solutions Pvt ltd is giving you Free access. Limited period offer Hurry-up Enroll Now at Udemy.


    Hurry-Up Enroll Now and get Free Access to the Entire Course


    ReplyDelete

Cloning database with RMAN

Below are the steps to clone production database(primedb) to UAT database(clonedb)   STEP1 :- Check the both servers are able to connect ...