以任意順序列印至少包含所有元音但不包含連續元音的行

以任意順序列印至少包含所有元音但不包含連續元音的行

問題是編寫一個腳本,該腳本接受一個文件作為參數,並打印包含 - 至少 - 所有元音的所有行, - 無論順序如何 - 但帶有 [沒有兩個相同的連續元音]。

例如 aeaiou 是允許的,但不允許 aaeiou 因為 'aa'

下面的腳本幾乎是我所需要的,但它不檢查連續性。

egrep -i '.[a]+' ${1} | egrep -i '[e]+' | egrep -i '[i]+' | egrep -i '[o]+'| egrep -i '[u]+'

注意:我可以使用 grep 和循環結構,但不能使用模糊/高級命令。

解決了;

egrep -vi '[a][a]' ${1} | egrep -vi '[e][e]' | egrep -vi '[i][i]' | egrep -vi '[o][o]' | egrep -vi '[i][i]' | egrep -i '[a]+' | egrep -i '[e]+' | egrep -i '[i]+' | egrep -i '[o]+'| egrep -i '[u]+'

答案1

只要您想排除整行(如果有雙元音),這應該有效:

grep -i a file | \
    grep -i e | \
    grep -i i | \
    grep -i o | \
    grep -i u | \
    grep -v -i '\([aeiou]\)\1'

答案2

grep您也可以使用一次調用sed,而不是多次調用

sed -rn '/(aa|ee|ii|oo|uu)/d; /a/{/e/{/i/{/o/{/u/p}}}}'

或者

sed -rn '/([aeiou])\1/d; /a/{/e/{/i/{/o/{/u/p}}}}'

如果您喜歡使用反向引用。

答案3

使用您的問題(稍作修改,在 之前有一個換行符號but not aaeiou because of the 'aa')作為輸入:

$ cat hehe.txt
The problem is to write a script that takes a file as argument
and prints all the lines that contain -at least- all the vowels ,
-regardless of ordering-, but with [no two same consecutive vowels].

e.g aeaiou is allowed, 
but not aaeiou because of the 'aa'

The script below is almost what I need, but it does not check for
consecutiveness.

$ awk 'BEGIN { IGNORECASE=1 };
       ! /aa|ee|ii|oo|uu/ && ( /a/ && /e/ && /i/ && /o/ && /u/ )' hehe.txt
The problem is to write a script that takes a file as argument
-regardless of ordering-, but with [no two same consecutive vowels].
e.g aeaiou is allowed, 

awk腳本啟用不區分大小寫的模式,刪除包含雙元音的行,然後列印包含每個元音至少一個的行。

相關內容