2

So I came up with two command:

1: grep -c [^[^aeiouAEIOU]] filename.txt; result:52331

2: grep -v -c [^aeiouAEIOU] filename.txt; result:52333

These two commands are same to me but with different result.

Please help, this make no sense to me.

HelenA
  • 96
  • 10
James
  • 41
  • 7

2 Answers2

2

Those two commands indeed do different things, and none of them actually does what you think:

grep -c [^[^aeiouAEIOU]] filename.txt

You can't really nest [^...] expressions. Instead, this regular expression is interpreted as [^[^aeiouAEIOU] concatenated with ]. That is, it will match anywhere in the line any combination of a character that isn't [, ^, or a vowel, and ]. For example, these strings will match: b], c], #], ;]; and these will not: a], E], c, d. And "anywhere in the line" means that the grep command above will count lines such as aaab], i uc], etc.

As for your second command,

grep -v -c [^aeiouAEIOU] filename.txt

Here you have double negation. First, [^aeiouAEIOU] matches any character other than a vowel. On the other hand, the -v option passed to grep inverts the match, so it will match any lines not matched by [^aeiouAEIOU]. The end effect is, basically, that you're telling grep: "Count all lines that don't have symbols other than aeiouAEIOU". That is, it will count only lines that contain only vowels.

Two commands that actually should give you the number of lines that don't start with a vowel are:

grep -c ^[^aeiouAEIOU] filename.txt
grep -v -c ^[aeiouAEIOU] filename.txt
Ivan A.
  • 409
  • 3
  • 9
0

You can try also this:

grep -Ewcv "*[aeiou]*" filename.txt
HelenA
  • 96
  • 10