-1

1)For instance, find any line that begins with a capital letter and ends with a period uses the following expression:

$ grep "^[A-Z].*\.$" demo.txt -- 1

what is the difference between grep '^[A-Z].*\.$' demo.txt and expression 1(using single quotes, I tried it and it seems the same)

what is the difference between grep '^[A-Z]*\.$' demo.txt and expression 1(what is the period for?)

2)To find each line in the file that contains an opening and closing parenthesis, with only letters and single spaces in between, uses $ grep "([A-Za-z ]*)" demo.txt, is there another way to represent the same thing using {}? something like $ grep "([A-Za-z ]\{...\})" demo.txt

3)Assume we want to search the contents of a text file called letter.txt, for the number of times a line began with one of these words: DO, DOO, or DOOO. How do we use the grep command(using only a single switch,cannot use them or, and using the single quotes) I tried something like this grep -c '^D(O){1,3}' letter.txt it seems not right.

Bob
  • 3
  • 4

1 Answers1

0

using single quotes, I tried it and it seems the same)

The quoting changes how the shell parses the arguments. See Difference between single and double quotes in Bash

what is the period for?)

To match any character. The * matches the preceding character or group zero or more times, so .* matches anything. Research what is a "regular expression". Regular expression is not shell globbing, see man 7 regex vs man 7 glob.

is there another way to represent the same thing using {}?

As far as I understand the question, no. ( is character represented by (, not by {, so I do not understand how to match ( with {...

DO, DOO, or DOOO. How do we use the grep command(using only a single switch,cannot use them or, and using the single quotes)

I would:

grep 'DO'

as that matches DOOO anyway. If you want to:

grep 'DO\|DOO\|DOOO'

but if you want to use bound you can just:

grep 'DO\{1,3\}'

like this grep -c '^D(O){1,3}' letter.txt it seems not right.

Sure, because ( matches a literal ( and { matches a literal {. If you want to represent a group in basic regular expression, escape the ( { characters.

# all these lines do the same.
grep 'DO\{1,3\}'
grep 'D\(O\)\{1,3\}'
grep -E 'D(O){1,3}'
grep -E 'DO{1,3}'

Research the difference between basic regular expression and extended regular expression.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Thank you. What does this mean `$ grep '^[A-Z]*\.$' demo.txt` without the period? I get it now that `.*` means anything but I think I am confused with regular expressions and wildcards `*` – Bob Feb 17 '21 at 18:34
  • `The * matches the preceding character or group zero or more times` the `[A-Z]*` matches `[A-Z]` zero or more times. – KamilCuk Feb 17 '21 at 19:18
  • the command `ls *.d?c` will list something like `something.d_c`. why the period here really means `.` – Bob Feb 17 '21 at 19:23
  • `ls *.d?c` the `*` means anything, why would we not use `.*`. Thanks for the help, it is just I feel like I messed somthing up – Bob Feb 17 '21 at 19:25
  • if `The * matches the preceding character or group zero or more times`, when I do the command `ls letter1[1-3]*.txt` `letter1.txt` will not be listed ([1-3] zero time – Bob Feb 17 '21 at 19:36
  • As I said: `Regular expression is not shell globbing, see man 7 regex vs man 7 glob`. In __shell globbing__ `*` matches anything, in __regular expression__ `*` matches zero or more preceding character or group. They differ __a lot__, and regular expression has also "flavors" - basic, extended and perl regexes. `grep` works with regex, your shell works with glob. – KamilCuk Feb 17 '21 at 20:29