RegExp Doubts

General chat concerning linux or linuxzoo.

Moderator: drgrussell

Post Reply
rotten

RegExp Doubts

Post by rotten » Wed Oct 01, 2014 5:17 am

Greetings,

I'm trying to write a grep command that prints all lines of a word file (represented by $WORDSFILE var) that begins with "something" and has precisely X (say 11) characters length.

I've managed with awk:

Code: Select all

 grep "^something" $WORDSFILE | awk '{ if (length() == 11) print $0}' 


But all my attempts using only grep seems to fail:

Code: Select all

--> grep -E "^something\{11\}" $WORDSFILE
--> grep -E "^something*\{11\}" $WORDSFILE
--> grep -E '^[something]\{11\}$' $WORDSFILE
--> grep '^something\{11\}$' $WORDSFILE
Could you kindly clarify the required regex syntax to accomplish this one using grep?

Thanks a lot for your support!
Best Regards,
rOttEna

drgrussell
Site Admin
Posts: 426
Joined: Sat Feb 12, 2005 8:57 pm
Are you a robot or a human?: Human

Re: RegExp Doubts

Post by drgrussell » Wed Oct 01, 2014 5:26 pm

grep -E "^something" | grep -E '^.{11}$'

otherwise you have to subtract the length of "something" from 11, and do "^something.{remainder}$" where remainder is the result of the calculation...

rotten

Re: RegExp Doubts

Post by rotten » Sun Oct 05, 2014 5:34 am

Thank you again, Mr. Gordon! That worked!

I'm having hard times constructing the regexp inside grep. For instance: when i want to match distinct character group, or subsets repetitions like the word "Berserker" which has two 'distinct' characters "er" repeating 3 times.. i'm using:

Code: Select all

 grep -i '\(\w\)\(\w\)[[:alnum:]]\+\1\2[[:alnum:]]\+\1\2' $DICTIONARY_FILE 
Which gives me a nice output when i run against the words file. 42 lines total, omitting some with ......).
  • Christchurch
    Ouagadougou
    Panamanian
    Panamanian's
    Panamanians
    Tanzanian
    Tanzanian's
    Tanzanians
    ancestresses
    confrontation
    confrontation's
    .............
    fingerprinting
    incinerating
    incriminating
    indoctrinating
    infringing
    inseminating
    insinuating
    intercontinental
    interlinking
    intermingling
    pinpointing
    possessiveness
    possessiveness's
    priestesses
    restlessness
    .............
But i'm under the impression i'm still missing something..

I appreciate your attention and i'm looking forward for you answer.

Best regards,
rOttEn

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests