簡體   English   中英

如何使用sed或awk替換一些具有相應數字表示的元音?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM