0

This question already has an answer here:

I am relatively new to Unix, and I stumbled across one curiosity. Some shell constructs like case or find, employ pattern matching, but it is not quite regex. Other commands, like ed, sed, vi, and awk use regular expressions for pattern matching. Could somebody list which shell commands (builtins, programs) use regex, and which use the other type of pattern matching?

marked as duplicate by Gilles shell Sep 24 '14 at 0:13

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I believe you can use regexes for find and case as well.. – ryekayo Sep 23 '14 at 15:18
1

case uses globs, which is a very simple pattern matching system similar to regular expressions. Some tools, like find, actually support both (via -name and -regex in this case). But the case is even more complicated: There are different flavours of regular expressions. Some tools support one, some several. You just have to check per tool and version combination what the man page or other reference documentation says (and even then it can be difficult to figure out). A reference list would be gigantic, and I don't think that would be a very useful answer.

  • Thank you for your answer, partucularly for great links! – Martin Sep 23 '14 at 15:37
1

I think the main difference between using regular expression is whether they require to match the whole string or not. In case, find and some other bash commands you have to match the whole string, while in sed, awk, grep and so on you have to match any part of the string. Other than that they are similar, but, of course, not identical.

For example, when you use regular expression in case statements of bash shell, it is assumed that your regular expression describes the whole string. I.e. (i'm using example here)

case $SERVER in
db-[0-9]+\.host\.com) echo "DB server"
;;
*)echo "Unknown server"
;;
esac

You can see that db-[0-9]+.host.com describes the string, which starts with "db-", then has one or more digits, and then ends with ".host.com", so db-1.host.com will match, while xdb-1.host.com will not.

Now if you look at sed, and write the search pattern in a similar way

echo "xdb-1.host.com"| sed -nr '/db-[0-9]+\.host\.com/p'

sed, unlike case command, WILL print the line xdb-1.host.com, because it can find the search pattern INSIDE this string. So, the idea is not to match the whole string, but to find any occurence of the pattern.

In a similar way, if you use regexp in find command, the whole string has to match. For example,

find / -regextype sed -regex ".*\.dat"

will find you all the files, which have the extension dat. But if you try to do the same search with sed,

find / | sed -nr '/.*\.dat/'

it will match all the files, which contain the string ".dat" in their file name.

There are also some minor syntax differences of course. For example if you do

find / -name "*.dat"

this is also a kind of regular expression, where * means "any number of arbitrary symbols", but in the strict sense regexp you should write ".*", where "." means any symbol, and * means any number of symbols of kind ".", so together meaning any number of any symbols.

  • thanks so much. we'll be greatful if you can add me points, because my 1 point is not enough for evaluating other people answers. sigh..) – John Smith Sep 23 '14 at 16:07
  • See Meta-Post on Reputation. You'll find that most people do it two points at a time... – eyoung100 Sep 23 '14 at 16:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.