![grep command linux grep command linux](https://www.tech-faq.com/wp-content/uploads/2009/02/Unix-Grep-Command-560x350.png)
Running the same command as above will result in this file being matched too: ~]$ ls -l /etc | grep "root *2" Now we add a new file to /etc that is called root2. Now we get all of the files we wanted to match. The following example will match the string root followed by 0 or more spaces: ~]$ ls -l /etc | grep "root *2"
![grep command linux grep command linux](https://thecustomizewindows.com/wp-content/uploads/2013/04/grep-command.png)
Asterisk is effectively a padding character as it applies a replication to the previous character. Some have 3, some have 4, some have 5, and so on. The challenge we have here is that we need the string root to indicate which number in the line we are trying to match (otherwise it would potentially match a 2 anywhere in the line and not just the size column), but we then have a varying number of spaces between the string root and the 2. The pattern “root 2” has exactly 3 spaces between the string root and 2. This matches only two of the lines we are after. The first command we put together is: ~]$ ls -l /etc | grep "root 2" rw-r-r- 1 root root 235 Feb 3 09:47 hosts.allow rw-r-r- 1 root root 2420 DIR_COLORS.xterm The files in /etc/ that matched this requirement at the time of carryimng out this challenge were as follows: In the following scenario we would like to match all long listing entries for files in /etc that have a size beginning with a 2. Search for a pattern in a specific “field” If we wanted to match just the lines that contain a number followed by “. We can see that the first 6 matching lines are matching on the number at the end of the modification time follwed by a space and the d from the first letter of the file/directory name. Lrwxrwxrwx 1 root root 10 rc5.d -> rc.d/rc5.d Lrwxrwxrwx 1 root root 10 rc4.d -> rc.d/rc4.d Lrwxrwxrwx 1 root root 10 rc3.d -> rc.d/rc3.d
![grep command linux grep command linux](https://bytexd.com/wp-content/uploads/2021/07/text-description-automatically-generated.png)
Lrwxrwxrwx 1 root root 10 rc2.d -> rc.d/rc2.d Lrwxrwxrwx 1 root root 10 rc1.d -> rc.d/rc1.d Lrwxrwxrwx 1 root root 10 rc0.d -> rc.d/rc0.d The following example matches all lines that contain a number in the range 1 to 6, followed by any single character, followed by a “d”. Search for a pattern containing a range of characters The following example matches all lines in the ps -ef output that end in bash: ~]$ ps -ef | grep "sh$" Search for a pattern at the end of a line The following example matches all lines in the ps -ef output that start with the string ptr: ~]$ ps -ef | grep "^ptr" Search for a pattern at the beginning of a line exit-with-session /etc/X11/xinit/Xclients" The following example matches all lines in the ps -ef output that have sh anywhere in them: ~]$ ps -ef | grep "sh" The back slash \ is an escape character.The asterisk * matches zero or more occurrences of the previous character.
![grep command linux grep command linux](https://i1.wp.com/www.linuxandubuntu.com/wp-content/uploads/2018/01/grep-help-command.png)