How to Use grep to Find Starting and Ending Pattern in Linux

The grep command is a useful way of searching for patterns inside a Text File in Linux. It can be used in many ways to filter our search and one of those is using the grep command to search for specific patterns inside a text file that starts and at the same time ends with a given character. When working with many text files in Linux, it usually gets hectic when it comes to retrieving or finding a specific set of words or specific sentences. The user or administrator gets frustrated when looking for those specific sentences manually throughout the whole text file, which may contain a lot of text inside.

Thus, when searching for specific text patterns or sentences inside a text file, it is useful to use the grep command along with search symbols for different searching purposes to retrieve the required text from the text file instead of manually scrolling through the text file and looking for it.

This article explains the use of grep command to find the sentences starting and ending with a specific pattern or character in Linux.

Using grep to Find Starting and Ending Pattern in Linux

The grep is a useful command in searching for specific patterns. It can be used to find a specific line in a Text File that starts with a specific pattern and can also be used to find the line in a Text File that ends with a specific pattern. Apart from listing the starting character or pattern and listing the ending character or pattern, the grep command is useful in listing the lines inside the text file that starts and at the same time ends with a specific character or pattern. The basic grep syntax to list line numbers that start and end with a specific starting and ending pattern or character is:

grep '^<starting-character>.*<ending-character>$' <file>

The grep command uses combination of special characters, mainly the asterisk (*), the ($) and the (^) symbols to filter the search pattern. In our case, to use the grep command to search for line starting and ending with specific pattern, we will use the “grepfile.txt” Text File containing the text below:

Now, to search for the line that starts with “L” and ends with “d”, the grep command along with the combination of these special characters used will be:

grep '^L.*d$' grepfile.txt

The “^” indicates that the Line or Sentence will start from the specified character provided after the symbol “^”, which in our case is “L”, and the “$” indicates that the sentence or line will end at the character provided before the symbol “$”, which in our case is the character “d”. The Terminal will highlight and list only that specific Sentence or Line if it exists in the Text File. In our case, as seen in the screenshot below, the Terminal will display the line and it can be seen that the sentence starts with “L” and ends with “d”:

Similarly, we can also search for other patterns in a Text File. In our case, now to search for the sentence or line that starts with the character “T” and ends with the character “.”, the grep command used now will be:

grep '^T.*.$' grepfile.txt

The Terminal will display the specific sentence starting and ending with the specified characters. In our case, as seen in the Terminal screenshot below, the command once executed will display the sentence starting with the character “T” and ending with the character “.”:

In our previous examples, we used the grep command to search for pattern starting and ending with a specified character using only one Text File, i.e. only one Text file that is provided in the command is being searched for the pattern. You can also use Multiple files as well. The grep command will search and list all the specified starting and ending character patterns in multiple files. Now, in our case, we used the text files “grepfile.txt” and “grepfile2.txt” to search for specific sentences that starts and at the same time ends with a specific character. To use the grep command in this case to search for multiple files, the command executed in the Terminal will be:

grep '^T.*.$' grepfile.txt grepfile2.txt

The Terminal will display the File Name along with the Sentences, if the sentence exists, in files that follow the same pattern, i.e. that starts with the specified character and ends with the specified character. In our case, running the previous command will display sentences from both the files that follows the pattern and it can be seen in the terminal screenshot below that only one sentence follows the searched pattern in “grepfile.txt”, while two sentences follow the searched pattern in “grepfile2.txt”, and both of the sentences are displayed in the Terminal along with their file names:

This is how you can search for sentences inside a text file or multiple text files that follow a specific pattern of starting and at the same time ending with a specified character.

Bonus Method 1: Using grep to search for Sentences with a Starting Character

Instead of searching for a sentence or line that starts and at the same time ends with a specific character, the grep command can also be used to only search for a sentence that starts with a specific character. The symbol “^” is used along with the grep command to indicate the grep should search for sentences starting with the character provided after the symbol “^”. The basic syntax of the grep command to search for sentences starting with a specific character is:

grep '^<starting-character>' <file>

In our case, to search for sentences starting with a specific character inside a text file using the grep command, we will use the “grepfile.txt” text file: 

Now, to search for the sentences inside the text file “grepfile.txt”, the grep command along with the starting character after the symbol “^” will be used. To search for sentences starting with the character “L”, the grep command in this case used will be:

grep '^L' grepfile.txt

The Terminal will display the sentences starting with the character “L”, if any sentences exist, and will highlight the starting “L” character in the Terminal. In our case, as you can see in the Terminal screenshot below, only two sentences start with the character “L” and both of the sentences are displayed in the Terminal along with the highlighted character “L” indicating the searced character:

In the previous example, we searched for the sentences starting with a specific character using only a single text file, i.e. only one given text file was searched. You can also use multiple files as well with the grep command. The grep command will use both the text files and will search for sentences in the Text File starting with a specific provided character. If any sentence exists, the sentence along with the text file name will be displayed in the Terminal. In our case, we have used the “grepfile.txt” and “grepfile2.txt” Text Files. To search for the sentences starting with the character “L” inside both the text files, the grep command in this case will be:

grep '^L' grepfile.txt grepfile2.txt

The Terminal will display the sentences starting with the specific character along with their file names. In our case, the “grepfile.txt” text file contains two sentences starting with the character “L” whereas the “grepfile2.txt” text file only contain one sentence starting with the character “L”. The Terminal Screenshot below highlights the character “L” in the sentences:

Similarly, you can search for other sentences starting with a specific character in Linux using the grep command.

Bonus Method 2: Using grep to search for Sentences with an Ending Character

In the previous method we have seen how we can use the grep command to search and list the sentences starting with a specific character in Linux. You can also use the grep command to search and list the sentences ending with a specific character as well using the grep command. The symbol “$” is used along with the grep command to indicate the grep should search for sentences ending with the character provided before the symbol “$”. The basic syntax of the grep command to search for sentences ending with a specific character is:

grep '<ending-character>$' <file>

In our case, to search for sentences ending with a specific character inside a text file using the grep command, we will use the “grepfile2.txt” text file. The “grepfile2.txt” text file has the content shown below in the screenshot: 

Now, to search for the sentences inside the text file “grepfile2.txt”, the grep command along with the ending character before the symbol “$” will be used. To search for sentences ending with the character “e” inside the text file “grepfile2.txt”, the grep command in this case used will be:

grep 'e$' grepfile2.txt

The Terminal will display the sentences ending with the character “e”, if any sentences exist, and will highlight the ending “e” character in the Terminal. In our case, as you can see in the Terminal screenshot below, only one sentence ends with the character “e” and the complete sentence is displayed in the Terminal along with the highlighted character “e” indicating the searced character:

Similarly, you can search for other sentences that end with a specific character. You can also search for sentences that end with a special character as well. In our case, in this example, we will search for the sentences that end with the special character “!”. We will use the same text file “grepfile2.txt” to search for the specific sentences ending with the special character “!”. The grep command executed in this case will be:

grep '!$' grepfile2.txt

The Terminal will display the sentences ending with the character “!”, and will highlight the ending “!” character in the Terminal as well. In our case, as you can see in the Terminal screenshot below, only one sentence ends with the character “!” and the complete sentence is displayed in the Terminal:

In the previous example, we searched for the sentences ending with a specific character using only a single text file, i.e. only one given text file was searched. You can also use multiple files as well with the grep command. The grep command will use both the text files and will search for sentences in the Text File ending with the provided character. If any sentence exists, the sentence along with the text file name will be displayed in the Terminal. In our case, we have used the “grepfile.txt” and “grepfile2.txt” Text Files. To search for the sentences ending with the character “e” inside both the text files, the grep command in this case will be:

grep 'e$' grepfile2.txt grepfile.txt

The Terminal will display the sentences ending with the specific character along with their file names. In our case, both the “grepfile.txt” and the “grepfile2.txt” text files contain only one sentence that ends with the character “e”. The Terminal Screenshot below highlights the character “e” in the sentences:

Similarly, you can search for other sentences ending with a specific character in Linux using the grep command.

Conclusion

The grep command can be used to search for text patterns in a single file. It can also be used to search for patterns in multiple files as well. The special character “^” can be used to retrieve words or sentences starting with a specified character along with the symbol. Also, the special character “$” can be used to retrieve words or sentences ending with a specified character along with the symbol. The special characters “^” and “$” when used in combination, can retrieve sentences in a Text File that start or end with the specified characters. This article explains all the scenarios in which the grep command can be used to retrieve specific sentences with a given pattern in Linux.