vortiproof.blogg.se

Using grep regex
Using grep regex






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

using grep regex

Lrwxrwxrwx 1 root root 10 rc4.d -> rc.d/rc4.d Lrwxrwxrwx 1 root root 10 rc3.d -> rc.d/rc3.d 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.The dollar sign $ at the end of a string matches the empty string at the end of a line.The caret ^ at the start of a string matches and the empty string at the beginning of the line.

using grep regex

  • list of characters enclosed by  matches any single character in that list (if first character is the caret ^ then it matches any character not in the list).
  • Here is a brief description of these special characters

    using grep regex

    The basic characters supported by grep are: Grep supports basic regular expression characters and the other two support some of the more more advanced regular expression characters. Linux and UNIX systems offer three variants of the grep command: The grep command is perfect in these situations and we explore some of it’s capabilities here. There will be many occasions when you are trying locate a specific set of lines in a file, such as a log file, or perhaps you are trying filter the results that have come back from a Linux or Unix command to just the ones relevant to your specific needs.








    Using grep regex