Skip to main content

Power of grep command in Linux.


 

The grep filter searches a file for a particular pattern of characters, and displays all lines that contain that pattern. The pattern that is searched in the file is referred to as the regular expression (grep stands for global search for regular expression and print out).


Syntax:

grep [options] pattern [files]
Options:
-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, case for matching
-l : Displays list of a 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.

-A n : Prints searched line and nlines after the result.
-B n : Prints searched line and n line before the result.
-C n : Prints searched line and n lines after before the result.


Grep commands:


grep command which stands for "global regular expression print" processes text line by line and prints any lines which match a specified pattern.


grep --version: gives the version of the grep package.


grep --help: this will give you all the options that can be used with grep command.


man grep: gives the manual of the grep command.


grep cat file1: gives the lines that match the word cat.


grep spathan /etc/passwd: gives the line which matches spathan.


cat /etc/group | grep spathan: gives the line which matches spathan


grep -c spathan /etc/passwd: gives the number of lines which matches spathan


grep -i spathan /etc/passwd: ignore lowercase and uppercase grep -n spathan /etc/passwd: gives the line numbers which match with spathan


grep -v spathan /etc/passwd: display everything except the line containing spathan.


grep -v spathan /etc/passwd | awk '{print $1}': display only 1st column of the lines which does not contain spathan word Is-1| grep -i desktop: displays the line which contains desktop directory.

......................... 

egrep: when you want to search for 2 words in the output.

 egrep -i "spathan l agowda" /etc/passwd: print all the lines which contains both words spathan and agowda.

Comments

Popular posts from this blog

Linux basic commands

 Linux basic commands: du  -sh  *  |  sort  -h  -r   |  head  -n  40  :    list out first 40 files in the directory that are taking more space in the directory.  cd : change directory Is-l listing the items in long listing format  pwd : print working directory Is-I format: type :no of links:owner : group:size :month :day :time :name cd/: go to/directory whoami: tells us by which username we are logged in. touch jerry: creates the file named jerry in present working directory. cp jerry lex: copy the content of jerry file and paste it to lex file. vi text1: creates the file text1 and open it in vi editor mkdir superman: creates the directory called superman mkdir abc def  : creates 2 folder in one command. touch filename wont work in /etc/ folder if logged in by normal account. man cp: shows manual for cp command. echo "india is my country"> file1 puts the text in file1. rm filename: remove the filename  mv lex luther renames the file from lex to luther  mv luther /h

patching tasks

 Patching a Linux system is a critical task to ensure that the system remains secure, stable, and up-to-date with the latest features and fixes. Here’s a comprehensive guide to the tasks involved in Linux patching: 1. Pre-Patching Preparation Backup System : Ensure you have a full system backup, including critical data, configuration files, and applications. Test the backup to verify its integrity. Check Disk Space : Verify that you have enough disk space, particularly on /var , /tmp , and /boot partitions. Review Current Patch Level : Determine the current patch level and installed packages using package management tools like yum , apt , dpkg , or rpm . Check System Logs : Review system logs to identify any issues that might affect the patching process. Test in a Staging Environment : If possible, apply patches in a staging environment that mirrors production to identify potential issues. Notify Stakeholders : Inform stakeholders about the scheduled maintenance window and expected do