![]() ![]() ![]() ![]() So here exact match means it will only search if the word is linux and not any other. Suppose you are searching for the string Linux then grep searches for all that word that contains Linux like itsmarttricks, linuxbooks, linuxtraining, and so on. Now you might think that what is an exact matching string. You can search for an exact matched string using grep command with argument -w. ~]# grep -i -c open file.txt # Searching for the count of Numeber of times the string is repeated in the File 3 Grep command with argument -c will show give you the count of the total number of times the string is repeated in the file. Print the Number of Matches found for searched string ~]# grep -n itsmarttricks/etc/passwd # Searching the Line Number Where the String is Available in the File 31:itsmarttricks:501:501::/home/itsmarttricks:/bin/bash Here I am searching for the user itsmarttricks in /etc/passwd file.Īs you can see on the sample output below it’s available online number 31 in /etc/passwd file. Grep command with argument -n will show you the line number where the string is available in the file. Print the Line Number of Searched String using grep command ~]# grep -r "linux" data/ # Searching Recursivelyĭata/test3.html:welcome to ĭata/testing/test3.html:welcome to ĭata/testing/test2.html:welcome to ĭata/testing/test1.html:welcome to ĭata/test2.html:welcome to ĭata/test1.html:welcome to Test1.html test1.txt test2.html test2.txt test3.html test3.txt Test1.html test1.txt test2.html test2.txt test3.html test3.txt testing So here I am going to search for the string Linux in directory data and also in all subdirectories contained by it using grep command with argument -r. data/testing) which also contains some txt files. Here I have a directory named data which contains some txt files with some content and also have a sub-directory named testing (i.e. To search for a string on a directory and also on all sub-directory contained by that directory recursively. ~]# grep -i '' file.txt # Searching using Regex Case Insensitively ~]# grep '' file.txt # Searching using Regex Like the number Regex, we can also set Regex for letters like a-z. ~]# grep '' test.txt # Searching using Regex means grep will search all those lines which contains any one number from 0 to 9. We can use grep command to Search by using regex. Search by Matching Number Regex using grep Command ~]# grep -v itsmarttricks file.txt # Search for Invert Match For example, here I am searching for the string itsmarttricks, In that case, grep will search only those lines which are don’t contains itsmarttricks string. Invert search means It will search only those lines which don’t contain the searched string. Search for Invert Match using grep commandĪnother nice feature we have in the grep command is inverted search. txt filesĪ.txt:linux is a open source operating system.ī.txt:Linux is a Open Source Operating System.Ĭ.txt:LINUX IS A OPEN SOURCE OPERATING SYSTEM. data]# grep -i linux *.txt # Searching from all. For example, here I am searching for string Linux from all available text files. We can use a wildcard to search with the grep command. Search for a String in Multiple Files using Wild Card ~]# grep -i 'linux\|itsmarttricks\|Open' file.txt # Search for Multiple Strings Linux, itsmarttricks, Open from file.txt. data]# grep -color -i linux a.txt b.txt # Searching from Multiple FilesĪ.txt: linux is a open source operating system.ī.txt: Linux is a Open Source Operating System. You can see in the output below we are able to search for the string Linux from both the files and the output show’s the searched string with the file name from which it searched. and I am going to search for string Linux from both the files. Here I have a directory named data which contains two files i.e. You can search for a string from multiple files. Search a String in Multiple Files using grep command ~]# grep -color -i linux file.txt # Searching for Case Insensitive Strings Linux is a Open Source Operating System. Here I am searching for string Linux and you will able to notice on the output below that I able to search all the strings whether it contains Uppercase or Lowercase. We can search Case Insensitive strings using grep command with argument -i. Search for Case Insensitive strings using grep command ~]# rpm -qa | grep vsftpd # Searching for a Installed Package ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |