Linux/Unix command Grep: Global Regular Expression Print

Hi Everyone, while working on any laptop with UNIX , one of the essential tasks one needs to do everyone time searches.
Search folder , search file , search type of file (.txt , .mp3 , .java) , search anything. So here we have grep command-line tool for our rescue. As the name suggests it’s the regular expression for text search pattern (don’t worry, I too didn’t get anything of this blah blah for the first time.)
I believe using is the best way to learn, so here I am jotting down all the possible causes and the way to use the command.

Syntax:

grep [options] pattern [files]

Options Description
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
-i : Ignores, the case for matching
-l : Displays list of filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern
-e exp : Specifies expression with this option. Can use multiple times.
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular expression (ERE)
-w : Match whole word
-o : Print only the matched parts of a matching line,
 with each such part on a separate output line.


Suppose, you have 5 folders in your user directory i.e (/home/USER_PROFILE_NAME). eg : mine is /home/bitsofjarvis/
1.) MOVIE (Batman-Series, Avenger-series, Hollywood, subtitles)
2.) Study ( AI-notes, Whatsapp-Notes, ONLINE-VIDEO-TUTORIAL)
3.) PICS (home, college, tour)
4.) MUSIC (trans, slow )

1.) List files/folder containing the string :
Best way to use is to ignore case , list all files with the line number and highlighted matched phrase.

grep -nlr "STRING_TO_SEARCH" .  // List files containing that string mentioned in quoates in current directory
grep -nlr "STRING_TO_SEARCH" . | wc -l  // File Count containing that string mentioned in quoates in current directory
ln -R | wc -l    // file count inside the directory & subdirectory
grep -nlr "STRING_TO_SEARCH" . | grep -v "bitsofjarvis" // Exclude & List files containing bitsofjarvis

awk options 'selection _criteria {action }' input-file > output-file 
awk '{print}' employee.txt  
	output:	// no pattern given , so awk rule applicable on all lines  
	ajay manager account 45000
    sunil clerk account 25000
    varun manager sales 50000
    deepak clerk sales 23000

awk '/manager/ {print}' employee.txt 
	output: //prints all the line which matches with the 'manager'
	ajay manager account 45000
	varun manager sales 50000
	amit manager account 47000 
    
 /*     
awk tokenize the line by words & each word is represent by $ & nth word
 eg : Bitsjarvis is best website
      $0 => whole line
      $1=> Bitsjarvis
      $2=> is
      $3=> best
      $4=> website      
*/
 awk '{print $1,$4}' employee.txt 
 	output:
	ajay 45000
	sunil 25000
	varun 50000
      
      

courtesy : GeeksForGeeks

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
error

Enjoy this blog? Please spread the word :)

0
Would love your thoughts, please comment.x
()
x