![](/img/trans.png)
[英]How to substitute spaces with %20 in a substring of a line across multiple files using sed, awk, grep etc
[英]How to substitute some vowels with corresponding number representation using sed or awk?
擁有一個包含多個(數百萬)電子郵件地址的文件,是否可以應用此轉換
a->4, e->3, i->1, o->0
對於所有電子郵件地址? 例如,那樣
test@example.com
被替換為t3st@3x4mpl3.c0m
?
我已經花了很多時間和精力,但發現用sed和regex技能完成它是不可能的。 這不是一個學校練習,它只是開源軟件時的隱私問題。
想象一下,數據是一個包含數百萬個電子郵件地址的日志文件。
請改用tr
命令:
$ tr 'aeio' '4310' <<< "test@example.com"
t3st@3x4mpl3.c0m
正如devnull
指出的,如果數據在文件中,你就可以做到
tr 'aeio' '4310' < myfile
你可以使用awk
cat file
this is a test here is an email my.test@email.com not this
Here are two email my@post.com and not.my@gmail.org
None here
然后用awk
awk '{for (i=1;i<=NF;i++) if ($i~/\./ && $i~"@") {gsub(/a/,"4",$i);gsub(/e/,"3",$i);gsub(/i/,"1",$i);gsub(/o/,"0",$i)}}1'
this is a test here is an email my.t3st@3m41l.c0m not this
Here are two email my@p0st.c0m and n0t.my@gm41l.0rg
None here
它是如何工作的:
awk '
{
for (i=1;i<=NF;i++) # Loop trough all fields in the string
if ($i~/\./ && $i~"@") { # If sting a field contains "." and "@" assume email
gsub(/a/,"4",$i) # Change the letter for the field
gsub(/e/,"3",$i) # Change the letter for the field
gsub(/i/,"1",$i) # Change the letter for the field
gsub(/o/,"0",$i) # Change the letter for the field
}
}1' file # Read the input file
使用bash
擴展user000001的解決方案僅修改電子郵件地址:
#!/bin/bash
while read -ra words; do
for word in "${words[@]}"; do
if [[ $word =~ ^.+@.*$ ]]; then
modwords+=( $(tr 'aeio' '4310' <<< $word) )
else
modwords+=( $word )
fi
done
echo "${modwords[@]}"
modwords=()
done < inputFile
輸出:
this is a test here is an email my.t3st@3m41l.c0m not this
Here are two email my@p0st.c0m and n0t.my@gm41l.0rg
None here
您可以將輸出重定向到另一個文件或執行< inputFile > tmp && mv tmp inputFile
。
sed 'y/aeio/4310/' YourFile
Tr會快得多但如果你只有sed ......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.