There are many ways to do this.
We show you some simple, reliable techniques.
Why Is This Useful?
fatmawati achmad zaenuri/Shutterstock.com
Searching a string for a smaller substring is a common requirement.
Linux users are blessed with any number ofutilities for manipulating text.
Some are built into the Bash shell, others are provided as standalone utilities or applications.
There’s a reason Unix-derived operating systems are richly served with string manipulation capabilities.
Related:How to Manipulate Strings in Bash on Linux
Some things thatappear to be filesare not simple files.
They’re special files representing things like hardware devices and sources of system information.
The abstraction performed by the operating system gives them the appearance and characteristics of files.
Text is also used as the input and output for commands in aterminal window.
This allows the redirection andpipingof input and output.
Here is a collection of simple techniques you’re able to include in your own scripts.
Copy this script into an editor, and save it to a file called “double.sh.”
This is a step that’s always required to make any script executable.
You’ll need to do this each time you create a script file.
Substitute the name of the appropriate script in each case.
Let’s initiate the script.
This works because the asterisk “*” represents any sequence of characters, including no characters.
In our example, there are characters in front of the substring.
These are matched by the first asterisk.
For flexibility, we can modify our script to handlevariablesinstead of literal strings.
This is script “double2.sh.”
Turning our little solution into a function will provide the most flexibility.
This is script “double3.sh.”
Weusedshoptwith its-s(set) option to setnocasematch, to make the matches case-insensitive.
Here’s how it runs.
We can use the trick of wrapping the substring in asterisks incasestatements, too.
This is “case.sh.”
The substring is found.
We canusegrep’s innate abilityto search for a string within a string to look for our substrings.
This script is called “subgrep.sh.”
We’re using the-q(quiet) option to stopgrepwriting anything to standard output.
Because zero equates totruein Bash, theifstatement is satisfied and thethenclause is executed.
Let’s see what its output is.
Finding Substrings With sed
We can usesedto find a substring, too.
By default,sedprints all text that is fed into it.
The only lines that are printed are matching lines.
This expression will print any lines that match or contain the value of $substring.
We feed the value of$stringintosedusing a here redirect,«<.
This is used to redirect values into a command in the current shell.
It doesn’t invoke a subshell in the way that a pipe would.
Related:How to Use the sed Command on Linux
The first-nis the test.
It will returntrueif the output from thesedcommand is non-zero.
The only way the output fromsedcan be non-zero is if a matching line was found.
If that’s the case,$substringmust have been found in$string.
This is “subsed.sh.”
We can test the logic of the script by editing the value of$substringso that the comparison fails.
Related:How to Use Case Statements in Bash Scripts