Score:2

bash: syntax error near unexpected token '|'

kw flag

Once logged in, use the "Find" utility to search within the current folder for the file whose name matches the following regex pattern:

  • lower-case letter
  • number
  • special character (% & # or -)
  • 2 lower-case letters
  • special character (@ : ? or ;)
  • number
  • uppercase letter

The command you will need to use is: find ./ -regextype posix-extendet -regex ^.*\/~~~~~~~ Where ~~~~~~~ is replaced with your regex string

I'm a high school student and we have coding challenges to do but I don't know much about code. I've been trying to do this challenge for a couple days now and came up with this string

find ./ -regextype posix-extended -regex ^.*[a-z]\d\%|&|#|-[a-z][a-z]\@|:|?|;\d[A-Z]

but a syntax error pops up when I tried it. I don't know how to fix it but still trying. If possible can anyone help?

hr flag
You should place quotes around your regular expression to protect it from interpretation by the shell
hr flag
Also be aware that the posix-extended regular expression type is unlikely to support PCRE features like `\d`
za flag
Please overlook my edit/layout. I interpreted "number" to mean "digit" in my answer - if you mean number, please elaborate about negative numbers, decimal fractions, hexadecimal numbers, scientific notated numbers and such (2.7e-14).
Raffa avatar
jp flag
Taking into consideration `find`'s output ... in `^.*\/------` : `.` needs to be escaped to match a literal period and `*` is not needed at all ... Should be `"^\.\/----"`
hr flag
@Raffa presumably the `.*/` at the start is to match leading path components, because `-regex` is a *whole path* match and the task is to match the basename (i.e. everything after the last `/`). The `^` doesn't really make sense though since a whole path match is implicitly anchored at both ends, and AFAIK `/` doesn't need escaping either for the regex or the shell.
Raffa avatar
jp flag
@steeldriver Ah, you are right … I was under the impression the max depth is only one level … Now reading the question again, I can’t see where I might have gotten that impression from … Probably I was misled by the start search path `./` when I shouldn’t.
Score:3
za flag

In almost all cases, it is necessary to mask your regex to prevent the shell from interpreting it and handing an already (partly) interpreted version to find.

find ./ -regextype posix-extended -regex "^.*[a-z][0-9][%&#-][a-z][a-z][@:?;][0-9][A-Z]"

Depending on your needs, a lowercase letter might include such characters as öäüö¢øßé, but I'm not sure, whether find accepts certain symbolic representations for those, without listing them all in square braces.

I went for square braces for [@:?;] etc. too, because it is shorter than the listing with OR-connection @|:|?|;. The @ doesn't need masking with backspace, as far as I'm aware of, as well as the percent sign.

A list of filenames to catch and a list of such not to catch would be helpful.

hr flag
You could consider using `[[:lower:]]`, `[[:digit:]]`, `[[:upper:]]` in place of `[a-z]`, `[0-9]`, `[A-Z]` which should both permit matching of characters like `öäüö¢øßé` and possibly prevent false matches in locales where the traditional ranges may encompass other characters.
hr flag
BTW you're right about `@` not needing to be escaped in the OR-connection, however `?` is a quantifier in posix-extended and **would** need to be escaped in that context i.e. `(@|:|\?|;)` - yet another reason to use `[@:?;]` here
Score:3
do flag

The syntax error you're receiving is because the regular expression needs to be surrounded by quotes, either single or double. Otherwise, the vertical bar character is treated as a Unix pipe, which redirects the Standard Out from the command preceding the pipe to the Standard In of the command following the pipe.

Once that is solved, however, you'll then receive another error: Invalid preceding regular expression

$ find ./ -regextype posix-extended -regex "^.*\/[a-z]\d\%|&|#|-[a-z][a-z]\@|:|?|;\d[A-Z]"
find: failed to compile regular expression '^.*\/[a-z]\d\%|&|#|-[a-z][a-z]\@|:|?|;\d[A-Z]': Invalid preceding regular expression

This is because you're not escaping the right characters. Out of those listed in the assignment, only the Question Mark needs to be escaped.

With regards to these special characters, the regular expression can be defined with either Character Classes (open and close brackets) or Alternates (with the vertical bar character). If Alternates, then they should be grouped within parenthesis. So you have two options:

$ find ./ -regextype posix-extended -regex "^.*\/[a-z]\d(%|&|#|-)[a-z][a-z](@|:|\?|;)\d[A-Z]"

Or the following. But note that because this version is using the Character Class brackets, you do not need to escape the Question Mark.

$ find ./ -regextype posix-extended -regex "^.*\/[a-z]\d[%&#-][a-z][a-z][@:?;]\d[A-Z]"

But this probably won't work as intended, because of \d. With POSIX Regular Expressions, you need to use either [0-9] or [[:digit:]]. Otherwise, \d will be interpreted as a literal "d". So that makes the final command as follows:

$ find ./ -regextype posix-extended -regex "^.*\/[a-z][0-9][%&#-][a-z][a-z][@:?;][0-9][A-Z]"

Or...

$ find ./ -regextype posix-extended -regex "^.*\/[a-z][[:digit:]][%&#-][a-z][a-z][@:?;][[:digit:]][A-Z]"

As a side note, to find two lower case letters, you can use [a-z]{2} instead of [a-z][a-z].

And if I may, as a final piece of advice...

When creating regular expressions or problem solving in general, it's best to start small and build from there. Change one thing at a time, ensuring that it's functioning as intended, and then when it breaks, figure out why it broke and adjust accordingly.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.