0

How to find the occurrences of three consecutive and identical word characters, for example "aaa", "bbb" etc using sed and grep?

I meant to find out words which have length 3 and are made from repeating characters, i.e. all characters of word must be same.

8
  • Should more occurences be reported? Such as aaaa
    – kos
    Jul 18 '15 at 19:07
  • No just 3 occurrence. Jul 18 '15 at 19:11
  • And of course you mean any word character, i.e. you don't want to explicitly input the characters to look for, right?
    – kos
    Jul 18 '15 at 19:26
  • I meant to find out words which hav length 3 and are made from repeating characters.,ie all characters of word must be same. Jul 18 '15 at 19:28
  • Eg aaa bbb rrr etc. I need to filter out such words from a file. Jul 18 '15 at 19:29
1

Using sed:

sed -n '/\(^\| \)\([A-Za-z]\)\2\2\($\| \)/p' file

Using grep:

grep '\(^\| \)\([A-Za-z]\)\2\2\($\| \)' file
  • \(^\| \): matches either the start of the line or a character;
  • \([A-Za-z]\): matches and groups any upper-case or lower-case alphabetical character;
  • \2: matches the previously grouped character;
  • \2: matches the previously grouped character;
  • \($\| \): matches either the end of the line or a character;
~$ cat file
aa word word
word wordaaaword word
aAa word word
aaa word word
word bbb word
word word ccc
aaaa word word
~$ sed -n '/\(^\| \)\([A-Za-z]\)\2\2\($\| \)/p' file
aaa word word
word bbb word
word word ccc
~$ grep '\(^\| \)\([A-Za-z]\)\2\2\($\| \)' file
aaa word word
word bbb word
word word ccc
15
  • Thanks worked. But If I have multiple such words in line. Then?? Jul 18 '15 at 19:55
  • @user2280915 It's the same, because a single match within the line is enough to print it. Just try it with an aaa aaa line
    – kos
    Jul 18 '15 at 19:56
  • Yeah worked perfectly. There is some glitch with grep. Just add * after space. (^| *) Jul 18 '15 at 19:59
  • @user2280915 If you want to match multiple spaces, use \+ instead of *, because * will match even 0 spaces, i.e. it will match an aaa string after any character, meaning you'll get also line containing words with say aaa at the end of the word (such as aaaa or nottobematchedaaa)
    – kos
    Jul 18 '15 at 20:45
  • No it will not because I first given space then used * this will going to work as +. It's like gg*. It will now check for atleast 1 g not 0 g. Jul 18 '15 at 20:48
1

In a situation like this I prefer annotation (sorry to change the question! ☺)

Instead of finding, this version marks the pattern with "".

sed -r 's/(\w)\1\1/"&"/g; 
        s/"(..(\w))"\2/\2\1/g'
  • s/(\w)\1\1/"&"/g; substitutes ...aaa => ..."aaa"
  • s/"(..(\w))"\2/\2\1/g fixes more than 3 chars ..."aaa"a => aaaa

example:

aa word wordaaaword word aAa word aaa word
word bbb word word ccc aaaa word word

outputs

aa word word"aaa"word word aAa word "aaa" word
word "bbb" word word "ccc" aaaa word word

Update (to cope with the new requirements in the question):

sed -r 's/\<(\w)\1\1\>/"&"/g' ex1

output:

aa word wordaaaword word aAa word "aaa" word
word "bbb" word word "ccc" aaaa word word
2
  • @kos, If I see it correctly, this ends up being simpler than the previous.
    – user216043
    Nov 4 '15 at 11:14
  • Well yes, there's no need to cope for sequences of the same character longer than 3. A single command and you're set :)
    – kos
    Nov 4 '15 at 11:21

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.