简体   繁体   中英

How to substitute some vowels with corresponding number representation using sed or awk?

Having a file with multiple (millions) email addresses, is it possible to apply this conversion

a->4, e->3, i->1, o->0 

for all email addresses present? So that for instance

test@example.com gets replaced with t3st@3x4mpl3.c0m ?

I have given it a lot of time and effort but find it impossible to accomplish with my sed and regex skills. It's not a school exercise, it's just a privacy concern when opensourcing software.

Imagine the data is a log file with millions of email addreses.

Use the tr command instead:

$ tr 'aeio' '4310' <<< "test@example.com"
t3st@3x4mpl3.c0m

As devnull pointed out, if the data is in a file, you can do

tr 'aeio' '4310' < myfile

You can use 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

Then with 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

How does it work:

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

Extending user000001's solution with bash to only modify email addresses:

#!/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

Output:

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

You can redirect the output to another file or do < inputFile > tmp && mv tmp inputFile .

sed 'y/aeio/4310/' YourFile 

Tr会快得多但如果你只有sed ......

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM