Score:0

Find specific numbers of capitalized words next to each other

ci flag

Let's say I have a .txt file:

Aaa
Aaa Bbb
Aaa Bbb Ccc

I want to find the row that has one capitalized word, two capitalized words, and three capitalized words. So one grep to find Aaa, one grep to find Aaa Bbb, and one grep to find Aab Bbb Ccc. This have to be done so the grep that finds Aaa Bbc doesnt find the one with Aaa Bbb Ccc (because the same words are in it). SO:

1 grep 
**Aaa**

2 grep
**Aaa Bbb**

3 grep
**Aaa Bbb Ccc**

This means that the first grep cannot find the Aaa in the second and third and so on. They have to work only if one, two or three letters start with a uppercase.

Kind regards Anders

bac0n avatar
cn flag
The last part contradicts what's shown in the example.
bac0n avatar
cn flag
`grep -Pwo '^([^a-z]*[A-Z]\w*\b){1}[^A-Z]*$' text.txt | grep -Ev '[A-Z]{3,}'` was the best I could, at least with grep don't think its possible to do with one grep
Anders Begtorp avatar
ci flag
I need to find names and count them. So I need to find how many `John`, `John Johnsen` and `John Johnson Johny` there are for instance.
Anders Begtorp avatar
ci flag
@bac0n your example does the job for the first John (and excludes the John Johnsen and John Johnson Johny which is nice!). Now I just need to find out the same for the other twos
Anders Begtorp avatar
ci flag
But by editing the {1} to {2} and {3} as well does the job! Thank you :-)
Anders Begtorp avatar
ci flag
@bac0n Do you know how to do it in rows? Like finding this (because it has tre rows staring with a capital letter): Aaa (newline) Bbb (newline) Ccc
bac0n avatar
cn flag
If I understand you right you could get the count from the second `grep -Evc '[A-Z]{3,}'`
in flag
This sounds like homework
Will avatar
id flag
Why do you want to do that?
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.