Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

grep is the de facto standard tool in Linux for searching patterns within a text file.

In this tutorial, we’ll explore a specific scenario of removing trailing newline from the grep output.

2. Understanding the Scenario

Let’s look at the sample.txt text file containing a few lines:

% cat -vet sample.txt
first line$
second line$
%

First, we must note that we’ve used the -vet option with the cat command to show non-printing characters such as newline character ($). Secondly, we’re using the % character as Bash prompt to distinguish it from the newline character ($).

Next, let’s use the grep command to match only those lines that contain the word “first”:

% grep -i 'first' sample.txt | cat -vet
first line$
%

Looking at the grep output, we can see the trailing newline character. In the following sections, we’ll visit this scenario while learning how to remove the trailing newline character from the grep output.

3. Using the tr command

We can use the -d option with the tr command to delete the newline characters from the output:

% grep -i 'first' sample.txt | tr -d '\n' | cat -vet
first line%

It looks like we got this right!

Alternatively, we can use the octal notation \012 for the line feed to get the same result:

% grep -i 'first' sample.txt | tr -d '\012' | cat -vet
first line%

Generally, it’s a standard convention to use the latter approach in Bash scripts.

4. Using the echo command

The echo command supports the -n option that we can use to remove the trailing newline character from the output. To use this strategy, let’s start by storing the grep output in the result variable:

% result=$(grep -i 'first' sample.txt)

Next, let’s use the -n option to instruct the echo command that we don’t want to print the trailing newline character:

% echo -n $result | cat -vet
first line%

We can see that the output is what we expected.

5. Using the head command

The head command supports the -c option to print a specific number of bytes. Additionally, we can specify a negative number to exclude those number of bytes from the output.

Now, as the newline character occupies one byte, we can use the -c option with -1 count to skip the last byte from the output:

% grep 'first' sample.txt | head -c -1 | cat -vet
first line%

We must note that we can use this strategy only when we’re sure about the presence of a trailing newline character. Otherwise, we’ll end up removing a valid character from the output.

6. awk as an Alternative

awk is an apt utility when working with text containing newlines because it relies on the newline character as a default record separator. Additionally, since awk uses the pattern-action paradigm for execution, we can easily search for a pattern, just like with the grep command.

Let’s go ahead and write a one-liner awk script to match the pattern and show the lines that match the output:

% awk '/first/ { printf("%s", $0);}' sample.txt | cat -vet
first line%

Perfect! We’ve got the expected result.

7. Conclusion

In this article, we learned multiple strategies to match a pattern using the grep command and remove the trailing newline from the output. Furthermore, we explored the awk command as an alternative to solve the same use case.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

guest
0 Comments
Inline Feedbacks
View all comments