1.

How Do I Perform A Case-insensitive Search?

Answer»

Several versions of sed support case-insensitive matching: ssed and GNU sed v3.02+ (with I flag after s/// or /regex/); sedmod with the -i switch; and sed16 (which supports both types of switches).

With other versions of sed, case-insensitive searching is awkward, so people may use awk or perl instead, since these programs have options for case-insensitive searches. In gawk/mawk, use "BEGIN {IGNORECASE=1}" and in perl, "/regex/i". For other seds, here are three solutions:

Solution 1: convert everything to upper case and search normally

# sed script, solution 1
h; # copy the original line to the hold space
# convert the pattern space to solid caps
 y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
# now we can search for the word "CARLOS"
/CARLOS/ {
# add or insert lines. NOTE: "s/.../.../" will not WORK
# here because we are searching a modified pattern
# space and are not printing the pattern space.
}
x; # get back the original pattern space
# the original pattern space will be printed
#---END of sed script---

Solution 2: search for both cases

Often, proper names will EITHER start with all lower-case ("unix"), with an INITIAL capital letter ("Unix") or occur in solid caps ("UNIX"). There may be no need to search for every possibility.

/UNIX/b match
/[Uu]nix/b match
Solution 3: search for all possible cases
# If you must, search for any possible combination
/[Ca][Aa][Rr][Ll][Oo][Ss]/ { ... }

Bear in mind that as the pattern length increases, this solution becomes an order of magnitude slower than the one of Solution 1, at least with some implementations of sed.

Several versions of sed support case-insensitive matching: ssed and GNU sed v3.02+ (with I flag after s/// or /regex/); sedmod with the -i switch; and sed16 (which supports both types of switches).

With other versions of sed, case-insensitive searching is awkward, so people may use awk or perl instead, since these programs have options for case-insensitive searches. In gawk/mawk, use "BEGIN {IGNORECASE=1}" and in perl, "/regex/i". For other seds, here are three solutions:

Solution 1: convert everything to upper case and search normally

# sed script, solution 1
h; # copy the original line to the hold space
# convert the pattern space to solid caps
 y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
# now we can search for the word "CARLOS"
/CARLOS/ {
# add or insert lines. Note: "s/.../.../" will not work
# here because we are searching a modified pattern
# space and are not printing the pattern space.
}
x; # get back the original pattern space
# the original pattern space will be printed
#---end of sed script---

Solution 2: search for both cases

Often, proper names will either start with all lower-case ("unix"), with an initial capital letter ("Unix") or occur in solid caps ("UNIX"). There may be no need to search for every possibility.

/UNIX/b match
/[Uu]nix/b match
Solution 3: search for all possible cases
# If you must, search for any possible combination
/[Ca][Aa][Rr][Ll][Oo][Ss]/ { ... }

Bear in mind that as the pattern length increases, this solution becomes an order of magnitude slower than the one of Solution 1, at least with some implementations of sed.



Discussion

No Comment Found