How can I write a shell script that will do a case-insensitive substring match of command output?
3 Answers
You can do case-insensitive substring matching natively in bash
using the regex operator =~
if you set the nocasematch
shell option. For example
s1="hElLo WoRlD"
s2="LO"
shopt -s nocasematch
[[ $s1 =~ $s2 ]] && echo "match" || echo "no match"
match
s1="gOoDbYe WoRlD"
[[ $s1 =~ $s2 ]] && echo "match" || echo "no match"
no match
-
7
-
4This option also affects the simple match operator.
[[ XYZ == xyz ]] && echo "match"
=>match
– itsadokDec 5, 2017 at 11:22
First here's a simple example script that doesn't ignore case:
#!/bin/bash
if [ $(echo hello) == hello ]; then
echo it works
fi
Try changing the string hello on the right, and it should no longer echo it works
. Try replacing echo hello
with a command of your choosing. If you want to ignore case, and neither string contains a line break, then you could use grep:
#!/bin/bash
if echo Hello | grep -iqF hello; then
echo it works
fi
The key here is that you are piping a command output to grep
. The if
statement tests the exit status of the rightmost command in a pipeline - in this case grep. Grep exits with success if and only if it finds a match.
The -i
option of grep says to ignore case.
The -q
option says to not emit output and exit after the first match.
The -F
option says to treat the argument as a string rather than a regular expression.
Note that the first example uses [ expression ]
which allows direct comparisons and various useful operators. The second form just execs commands and tests their exit status.
-
I don't understand why Gilles felt it was necessary to change the code I contributed. He didn't break anything but it worked just fine. You don't need the double quotes in this example - they are important if the output contains spaces however. And == works just as well as = because sh is actually bash on Linux. The original Bourne Shell is long gone at this point in time. I don't think even Solaris ships it any more. While unnecessary in this example I agree that double quotes are probably a best practice, but so is '==' in my opinion, to keep assignment and comparison clearly separate. May 29, 2014 at 2:58
-
-
With sufficient reputation, yes. I would hope somebody with high reputation would think twice before making needless edits however, particularly to code in this forum. unix.stackexchange.com/help/privileges May 29, 2014 at 3:22
-
@BobDoolittle It may be in certain cases it makes a difference but not with your setup - it's good to know.– user44370May 29, 2014 at 5:01
-
2Note that in practice, it's not only about the Bourne shell.
==
is not POSIX.sh
is notbash
on all Linux based systems.==
is not supported byash
(upon which thesh
of many BSDs and Debian derivatives at least is based), orposh
, and needs quoted inzsh
. There's no point doubling the=
.[
is a command for testing. There's no need to disambiguate between assignment and comparison here. That's different in(( a == b ))
vs(( a = b))
. Using==
in a script that starts with#! /bin/sh
is wrong. If you assumeksh
orbash
syntax, update the#!
accordingly. May 29, 2014 at 11:39
For a case-sensitive string search of the value of the variable needle
in the value of the variable haystack
:
case "$haystack" in
*"$needle"*) echo "present";
*) echo "absent";
esac
For a case-insensitive string search, convert both to the same case.
uc_needle=$(printf %s "$needle" | tr '[:lower:]' '[:upper:]' ; echo .); uc_needle=${uc_needle%.}
uc_haystack=$(printf %s "$haystack" | tr '[:lower:]' '[:upper:]' ; echo .); uc_haystack=${uc_haystack%.}
case "$uc_haystack" in
*"$uc_needle"*) echo "present";;
*) echo "absent";;
esac
Note that the tr
in GNU coreutils doesn't support multibyte locales (e.g. UTF-8). To make work with multibyte locales, use awk instead. If you're going to use awk, you can make it do the string comparison and not just the conversion.
if awk 'BEGIN {exit !index(toupper(ARGV[2]), toupper(ARGV[1]))}' "$needle" "$haystack"; then
echo "present"
else
echo "absent"
fi
The tr
from BusyBox doesn't support the [:CLASS:]
syntax; you can use tr a-z A-Z
instead. BusyBox doesn't support non-ASCII locales.
In bash (but not sh), version 4.0+, there is a built-in syntax for case conversion, and a simpler syntax for string matching.
if [[ "${haystack^^}" = *"${needle^^}"* ]]; then
echo "present"
else
echo "absent"
esac
-
I realize this is a couple years old, but all that
printf | tr
makes my head spin around. Where possible, keep your invocation of commands to a minimal ... given a variable v, you can accomplish the same thing usingv=$(tr '[:lower:]' '[:upper:]' <<<$v)
. For those who have never seen it before, the<<<
is essentially a "here variable" like the use of<<EOF
is for a here document. Don'tprintf
orecho
unless you absolutely have to do so.– WillJan 11, 2017 at 21:33 -
@Will That only works in shells that have the
<<<
operator: ksh, bash, zsh, but not plain sh. And it's pretty close to piping fromprintf
in terms of how it runs: there's the same number of calls tofork
andexecve
(assuming thatprintf
is built-in, which is the case on most common shells); a difference is that<<<
creates a temporary file rather than using a pipe.<<<
is convenient to type but not a performance improvement. Jan 11, 2017 at 21:45
grep -i
may be?bash
, which is a superset of the unix standardsh
. You might start by looking at one of these: |1| |2| -- just to get a grip on what the actual context is.