0

I am trying to find two consecutive words that start with capital letters.

Example

Input: x Yyy Zzz xx y

Output: Yyy Zzz

Right now I'm able to find all the capital letters cat txtfile.txt | grep -o '\<[A-Z][a-z]*\>'

How to alter the code so I can get the output?

Kind regards

1
  • Are you trying to grep entire lines? Or just consecutive capitalized words? Your sole example would suggest the latter. Also, do you need word pairs returned, or all consecutive capitalized words (2 words, 3 words, etc.) returned, whatever number in a row? Oct 20 '21 at 4:57
2

Using Raku (formerly known as Perl_6)

raku -ne 'my @a = .words.rotor(2 => -1); for @a {.put if $_ ~~ $_.map(*.wordcase)};'  

Sample Input:

x Yyy Zzz xx y
x Yyy Zzz Www
a Mmm: Yyy bbb
aaa aaa aaa
Ccc ccc CCC
Bbb Bbb Bbb Bbb

Sample Output:

Yyy Zzz
Yyy Zzz
Zzz Www
Mmm: Yyy
Bbb Bbb
Bbb Bbb
Bbb Bbb

Calling .words instructs Raku to split input lines on whitespace. These words are taken and rotor-ed together. The rotor parameter (2 => -1) instructs words to be taken as adjacent pairs with an overlap such that each consecutive overlapping pair of words is created.

Word pairs are loaded into the $_ topic variable, and tested to see if they match against $_.map(*.wordcase), in other words, tested to see if they match a version of themselves wherein each word is (initial letter) capitalized. If a match is found then $_ must be pairs of (initial letter) capitalized words already, and such word pairs are returned.

Note, if a linewise return is required the .put call can be replaced by (for example) print "$_, ". Also Raku has a unique routine if only unique word pairs are desired.

https://docs.raku.org/routine/wordcase
https://docs.raku.org/routine/rotor
https://raku.org

2

Taking Quasimodo's examples and other possible cases, and assuming punctuations must match, and also more than two consecutive words capitalized, using GNU grep:

$ cat file
x Yyy Zzz xx y
x Yyy Zzz Www
a Mmm: Yyy bbb
aaa aaa aaa
Ccc ccc CCC
Bbb Bbb Bbb Bbb
$ grep -P '[A-Z][^ ]*(?: +[A-Z][^ ]*)+' file
x Yyy Zzz xx y
x Yyy Zzz Www
a Mmm: Yyy bbb
Bbb Bbb Bbb Bbb

  • [A-Z][^ ]* matches words capitalized followed by any character that is not a space.
  • (?: +[A-Z][^ ]*)+ matches one or more spaces followed by the mentioned pattern repeated once or more times.

As suggested by @cas there's this alternative:

Using -z will detect sequential capitalised words even across line boundaries (e.g. CCC\nBbb). and using \s instead of just space will make it work with tabs and other whitespace too.

grep -z -P '[A-Z][^\s]*(?:\s+[A-Z][^\s]*)+' file
0
1

This should do the job:

cat txtfile.txt | grep -o '[A-Z][a-z]* [A-Z][a-z]*'
6
  • 1
    Thank you so much! Do you know how it would look if the first word can't be the first word of a sentence? So you find two consecutive words with capital letters from the second word in a sentence and forward? Oct 19 '21 at 20:02
  • @AndersBegtorp, if I understood your question correctly (and you don't mind using grep 2x) you can use: cat txtfile.txt | grep -v '^[A-Z][a-z]' | grep -o '[A-Z][a-z]* [A-Z][a-z]*' If you meant on: grep shouldn't match line that starts with word starting with capital letter. If you meant on: grep shouldn't match line that starts with two consecutive words starting with capitals: cat txtfile.txt | grep -v '^[A-Z][a-z]* [A-Z][a-z]*' | grep -o '[A-Z][a-z]* [A-Z][a-z]*
    – Damir
    Oct 19 '21 at 20:22
  • 1
    @AndersBegtorp Welcome, if you have other requirements edit the question adding them, don't do it in comments. Also add aclarations about Quasimodo's alternative examples. Oct 19 '21 at 21:15
  • @Damir This doesn't quite work take the string Edge CaSe or I Started and apply your solution to them...
    – JPI93
    Oct 20 '21 at 17:54
  • 1
    @JPI93, thanks, I know, but saw that others gave good examples so I didn't continue fixing my own. It's the same with EDge Case or EdGe Case, or... :). It would be great that OPs in general put more details, or their 'Inputs' with more different lines (more different cases)... However, thanks, I will improve my answer soon...
    – Damir
    Oct 20 '21 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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