Character class exclusion
I have Work1, a file of English words.
As a learning exercise I coded this ... Code:
echo "Find 5-character words which have the same letter in positions 1 and 3." To make the exercise more interesting I coded this ... Code:
echo "Find 5-character words which have the same letter in positions 1 and 3" Please advise. Daniel B. Martin |
I found a solution but it is a little out there. Also my reference was posted over 5 years ago so there may be an alternative now.
Anyhoo, here is what worked: Code:
grep -P '^(.)(?:(?!\1).)\1(?:(?!\1).)(?:(?!\1).)$' word_file I will be interested to see if there is an alternative or even a way to shorten the current solution :) |
Well, this is not shorter, but (IMO) more readable (basically a straightforward translation of each condition into awk):
Code:
awk -F '' 'NF == 5 && $1 == $3 && !index($2 substr($0, 4), $1)' input |
You can make it shorter by removing the (?: ... ) groups
Code:
grep -P '^(.)(?!\1).\1(?!\1).(?!\1).$' words |
Quote:
Daniel B. Martin |
Quote:
Code:
grep -P '^(.)(?!\1).\1(?!\1).(?!\1).$' words Code:
grep -P enable perl regular expression |
And a little more tidy up :)
Code:
grep -P '^(.)(?!\1).\1((?!\1).){2}$' word_file |
Thank you to keefaz and grail for thoughtful contributions. This thread is marked SOLVED!
Daniel B. Martin |
All times are GMT -5. The time now is 11:42 PM. |