1. Overview

When working with the Linux command line, a common operation is checking if a string contains another substring.

In this tutorial, we’ll look at several techniques to complete this task. First, we’ll review some shell built-in commands. Then, we’ll explore several tools that come pre-installed on Linux systems.

2. Making Use of the Shell Builtin Commands

The shells have several tools that can help us to perform string evaluations. For example, the Bash shell has the [ ] builtin commands and the [[ ]] keywords.

These tools can perform string evaluations that we can use to search for a string inside another.

Let’s say we want to find the substring “apple” within the string “pineapple”, with the help of the [[ ]] keyword and the = operator:

$ [[ "pineapples" = *"apple"* ]] && echo "Found it :)"
Found it :)

Here, we’re instructing the shell that the string to the right of the = operator will be considered a pattern. The special character “*” surrounding the word “apple”, will match any string.

Similarly, we can use the =~ operator:

$ [[ "pineapples" =~ "apple" ]] && echo "Found it again :)"
Found it again :)

In this scenario, we instruct the shell that the string to the right of the =~ operator will be considered a POSIX extended regular expression. Therefore, this expression will search for a match within the word on the left.

Next, let’s try with the builtin command [ ], to avoid bashisms and make it more portable:

$ [ -z "${pineapples##*apple*}" ] && echo "Found it again :)"
Found it again :)

In this scenario, we use the parameter expansion to remove everything that matches the “apple” substring. If the match is successful, then the result will be empty, and the exit status of the test [ -z … ] will be true.

Finally, we can use the case keyword:

$ case pineapples in 
    *apple*) echo "Found it :)";; 
    *) echo "Not Found";; 
  esac
Found it :)

In this example, we’re performing a pattern matching on the “pineapples” string. If the string “apple” is matched, the pattern between the delimiter “;;” will be executed.

3. The grep Command

The grep command is a great tool for searching. Let’s try with a simple pattern matching:

$ grep -q "apple" <<< "pineapples" && echo "Found it with grep" 
Found it with grep

Here, we’re using a here-string, “<<<“, to feed “apple” into grep. Additionally, we can use a pipe to avoid the bashism:

$ echo "pineapples" | grep -q "apple" && echo "Found it with grep"
Found it with grep

In both cases, we used the parameter -q because we want only the exit status.

4. The sed Command

Additionally, we can use the sed utility to match against a simple regular expression:

$ [ -n "$(sed -n '/apple/p' <<< 'pineapples')" ] && echo "Found it with sed"
Found it with sed

Here, we’re searching for the pattern that contains the word “apple”. If sed finds it, then it will expand the command substitution with the result.

Next, the -n operator of the builtin [ ], will have a successful exit status if the length of the string returned is non-zero.

Similar to the grep scenario, we can apply the same strategy by using a pipe to make it portable:

$ [ -n "$( echo 'pineapples' | sed -n '/apple/p')" ] && echo "Found it with sed"
Found it with sed

5. The awk Command

awk is another powerful tool that we can use to search for the substring.

Let’s try with a simple pattern:

$ echo "pineapple" | awk '!/apple/{exit 1}' && echo Found
Found

Here, we just search for the string “apple”. Then, if awk couldn’t find it from the string, we force the exit with a custom status error.

Also, we can make use of internal awk functions. Let’s use the match function:

$ awk \
    -v my_string="pineapples" \
    -v my_substring="apple" ' \
    BEGIN{ if (match(my_string, my_substring)) print "Found it with awk" }'
Found it with awk

Here, we passed both the string and the substring in the my_string and my_substring as variables, respectively.

Later, inside the BEGIN pattern with the match function, we check if the content of the variable my_substring is within the my_string variable.

6. Searching With Perl

Perl is another non-interactive powerful tool that we can use to process text.

We can use the Perl interpreter in a similar way to our awk scenario. In other words, we can define custom variables to store our strings.

Let’s try with the “- -” parameter following with our custom parameter names. Let’s call them -my_str and -my_sub for the string and the substring, respectively:

$ perl -se 'print "Found it with Perl\n" if $my_str =~ /$my_sub/' -- -my_str=pineapple -my_sub=apple
Found it with Perl

Here, inside the script, Perl print the message only if the content of the variable my_str matches with the regular expression defined on the right of the =~ operator.

We’re able to achieve this because the parameter -s that enables the creation of variables defined them as command arguments.

7. Conclusion

In this article, we’ve addressed different approaches to check if a string contains a specific substring.

We’ve reviewed some shell builtins and the utilities grep, sed, awk, and Perl that comes pre-installed on most Linux systems.

guest
0 Comments
Inline Feedbacks
View all comments