繁体   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