Back to Blog
Ubuntu grep command examples7/7/2023 ![]() $ grep -no-filename file test.txt test1.txtīoth of the above commands are equivalent so you can choose whichever you want. The following Linux grep commands will illustrate this for you. Often you may want to ignore them or omit them altogether. If you run the examples of the second command again, you’ll notice that grep prefixes the output with the filenames. $ grep -line-number -w cat /usr/share/dict/american-englishīoth the above commands print the lines that contain the word cat in the american-english dictionary. ![]() $ grep -n -w cat /usr/share/dict/american-english Take a look at the below commands to understand how this works. It utilizes a 1-based index where the first line of the file is line number 1 and the tenth line is line number 10. You can instruct grep to display the line numbers where a match has been found. The commands return 5 and 2 respectively. $ grep -c -w hello /usr/share/dict/american-english $ grep -c hello /usr/share/dict/american-english We’ve added this flag to the above commands to help you visualize how this works. When it is used, grep returns the number of matches instead of printing the lines. The -c option is very handy in such situations. Often you might simply want the number of matches found using some pattern. In my system, the first command returns 5 lines whereas the second command only two. If you run them one after another, you’ll see the difference. $ grep -w hello /usr/share/dict/american-english $ grep hello /usr/share/dict/american-english You can easily do this using the -w option, as shown below. Often you’ll want to discard these values. So it will also print lines that have the pattern inside arbitrary words or sentences. The grep utility prints any line containing the pattern. The above commands are equivalent and print only those lines that do not contain the file. Check out the below command for a quick view. It means that grep will print all lines that do not contain the given pattern. The grep utility allows users to invert matching. The last command is obsolete so avoid using that one. It should also be the same in your machine. This returns an extra line of output in my terminal. You can tell grep to ignore the case of the pattern in such cases, as demonstrated below. Sometimes the user might not be sure of the pattern’s case. Ignore Case Matchingīy default, grep will search for the given pattern in a case sensitive manner. If no match is found, the output will be empty and grep will terminate. This command will output the value of $USER as many times grep encounters it. So, grep will only output the specified patterns, if it exists. You can suppress this output and tell grep to display only the matched portion. Print Matched Portion Onlyīy default, grep will display the entire line containing the pattern. Now grep will print all lines containing file from both of the files. We’ve copied test.txt and created another file containing the same lines but named test1.txt. Simply provide all filenames separated by whitespace after the pattern. You can use grep to print lines containing specific patterns from more than one file at the same time. The second command will display all such lines that contain your username. The first example will display all lines containing root in the /etc/passwd file. Simply enter the pattern followed by the filename after grep, as shown below. One of the basic use of grep in Linux is to locate lines containing specific information from files. It will help you learn more advanced commands in the future. We demonstrate some basic grep examples to help you familiarize yourself with this tool. Since grep command allows users to dig out information using a plethora of combinations, starting users are often confused with its usage. Additionally, we’ve also leveraged the /etc/passwd file. You can copy the text from here and use the same filename for practicing. We named it test.txt and have used in for many grep examples. ![]() We’ve also created a simple text file containing the following. We’ve used the american-english file found here for some of our demonstration purposes. Most Linux distributions should contain some dictionary files in the /usr/share/dict directory. Since Linux grep utility works on files, we’ve outlined some files you can use for practicing. ![]() Demo Files for Illustrating Linux grep Commands ![]()
0 Comments
Read More
Leave a Reply. |