我有這樣的輸入
sie%Qu7s Kuux"oh9 ohc9ahG% hoe8Toh: Eix*ohd1 doh:bo2U Cu0doo|t zo`L9xaW
fie5Du[h Phe8aid# Opu&fai5 ieZ<aek6 hu4ga&Di Oose}p1p aiD@oos2 nu-a1Fub
ahqu5To/ ahtie[H3 ioK&u5Ai nei1Za#d poo_Th9r gu|aGh7h uZ%io2ah IeNah&v7
eif\e8AE Ieb,ing4 reph1oW* eeSh'ee8 Ah ei4ai Oi0Ca,vu Esh1xe?e Wei&k4ic
ue5OhQu. aaf-i8uP eedae%T5 sei?M9Pu ieH[oh2l ieh~ah8A aev"oo9A Ohf"i8de
Foh:x2zi aLoo'qu2 Ia6aig-e La{vie1E IeFoh{cI Au_h7Hee Se)f4ebi Cah$yu7m
其中列中的每個單詞構成一個密碼 ì我試圖列印任何單詞以相同字母開頭和結尾的行,因此我們不區分大寫和小寫字母
我知道用命令 grep 我可以做到這一點
cat passwords.txt | grep -e ' \([A-Z]\)......\1 ' -e ' \([a-z]\)......\1 '
但在這里這個詞只會以相同的后者(大寫或小寫字母)開始和結束,比如
Foh:x2zi aLoo'qu2 Ia6aig-e La{vie1E IeFoh{cI Au_h7Hee Se)f4ebi Cah$yu7m
預期產出
eif\e8AE Ieb,ing4 reph1oW* eeSh'ee8 Ah ei4ai Oi0Ca,vu Esh1xe?e Wei&k4ic
sie%Qu7s Kuux"oh9 ohc9ahG% hoe8Toh: Eix*ohd1 doh:bo2U Cu0doo|t zo`L9xaW
ue5OhQu. aaf-i8uP eedae%T5 sei?M9Pu ieH[oh2l ieh~ah8A aev"oo9A Ohf"i8de
Foh:x2zi aLoo'qu2 Ia6aig-e La{vie1E IeFoh{cI Au_h7Hee Se)f4ebi Cah$yu7m
ahqu5To/ ahtie[H3 ioK&u5Ai nei1Za#d poo_Th9r gu|aGh7h uZ%io2ah IeNah&v7
uj5u.com熱心網友回復:
使用 GNU grep:
grep -i -P '(?<!\S)(\S)(?:\S*\1)?(?!\S)' passwords.txt
該-i選項打開不區分大小寫,-P打開 PCRE 風格(支持lookbehinds/lookaheads)。
請參閱正則運算式證明。
解釋
--------------------------------------------------------------------------------
(?<! look behind to see if there is not:
--------------------------------------------------------------------------------
\S non-whitespace (all but \n, \r, \t, \f,
and " ")
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
\S non-whitespace (all but \n, \r, \t, \f,
and " ")
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
\S* non-whitespace (all but \n, \r, \t, \f,
and " ") (0 or more times (matching the
most amount possible))
--------------------------------------------------------------------------------
\1 what was matched by capture \1
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
\S non-whitespace (all but \n, \r, \t, \f,
and " ")
--------------------------------------------------------------------------------
) end of look-ahead
uj5u.com熱心網友回復:
使用 GNU grep:
grep -iE '(.)[^ ]{6}\1' passwords.txt
輸出:
sie%Qu7s Kuux"oh9 ohc9ahG% hoe8Toh: Eix*ohd1 doh:bo2U Cu0doo|t zo`L9xaW
ahqu5To/ahtie[H3 ioK&u5Ai nei1Za#d poo_Th9r gu|aGh7h uZ%io2ah IeNah&v7
eif\e8AE Ieb,ing4 reph1oW* eeSh'ee8 Ah ei4ai Oi0Ca,vu Esh1xe?e Wei&k4ic
ue5OhQu。aaf-i8uP eedae%T5 sei?M9Pu ieH[oh2l ieh~ah8A aev"oo9A Ohf"i8de
Foh:x2zi aLoo'qu2 Ia6aig-e La{vie1E IeFoh{cI Au_h7Hee Se)f4ebi Cah$yu7m
-i: 忽略模式和輸入資料中的大小寫差異,以便僅大小寫不同的字符相互匹配。
-E: 解釋(.)[^ ]{6}\1為擴展的正則運算式。
uj5u.com熱心網友回復:
使用您顯示的示例,請嘗試以下awk程式。用 GNU 撰寫和測驗awk,應該可以在任何awk.
awk '
{
for(i=1;i<=NF;i ){
if(tolower(substr($i,1,1))==tolower(substr($i,length($i)))){
print
next
}
}
}
' Input_file
解釋:簡單的解釋是,遍歷所有行中的所有欄位(列)并比較它們的第一個和最后一個值(每個欄位的),如果它們相等,則列印該行并移至下一行,依此類推。
獎勵解決方案:只需對上述解決方案稍作調整,如果您有 GNUawk則使用IGNORECASE,然后您可以將上面的代碼縮短為以下代碼。
awk -v IGNORECASE="1" '
{
for(i=1;i<=NF;i ){
if(substr($i,1,1)==substr($i,length($i))){
print
next
}
}
}
' Input_file
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/334532.html
