this line is the 1st lower case line in this file. Here’s an example of the WORD grep command, which only looks for the word “is.” Please note that this output does not have the line “This Line Has All Its First Characters With Upper Case,” even though “is” is in “This,” because the next line is only looking for the word “is” and not for “this.” $ grep -iw "is" demo_file THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE. This Line Has All Its First Character Of The Word With Upper Case. $ grep -i "is" demo_file THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE. In the following example, grep is looking for the word “is.” When you search for “is,” it will show you “is,” “his,” “this,” and anything else that has “is” as a substring. All the lines will show up if you just do a normal search. Use the -w option if you want to search for a word and don’t want it to match any substrings. grep -w is used to look for full words, not just parts of words. $ grep "lines.*empty" demo_file Two lines above this line is empty. i.e To search “linesempty” in the demo_file. In the following example, it searches for all the pattern that starts with “lines” and ends with “empty” with anything in-between. This is a very powerful feature, if you can use use regular expression effectively. Match regular expression in files Syntax: grep "REGEX" filename $ grep -i "the" demo_file THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE. So it matches all versions of words like “the,” “THE,” and “the,” as shown below. This looks for the given string or pattern without caring about the case. Case insensitive search using grep -i Syntax: grep -i "string" FILE demo_file1:Two lines above this line is empty. demo_file1:this line is the 1st lower case line in this file. demo_file:Two lines above this line is empty. $ cp demo_file demo_file1 $ grep "this" demo_* demo_file:this line is the 1st lower case line in this file. When the meta character is seen by the Linux shell, it does the expansion and gives all the files to grep as input. As shown below, the grep output will also include the name of the file before each line that matches the specific pattern. Let’s copy the demo_file to the demo_file1 for this example. This is another simple way to use the grep command. Checking more than one file for the given string. Syntax: grep "literal_string" filename $ grep "this" demo_file this line is the 1st lower case line in this file. The most basic way to use the grep command is to look for a specific string in a file. $ cat demo_file THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE. Both beginners and experts will find these examples very useful.įirst, make the following demo file, which will be used to show how to use the grep command in the examples that follow. In this article, we’ll look at 7 examples of how the Linux grep command can be used in real life. We’ve already talked about 7 real-world uses for the Linux find command, the Linux command line history, and the mysqladmin command. This is part of a series called “7 Examples,” in which 7 detailed examples are given for a certain command or feature. You should learn how to use the grep command in Linux.
0 Comments
Leave a Reply. |