Using awk/sed to flip around some email address?
try:
awk -F'@' '
{ split($2, flip, ".");
for (i=length(flip); i>=1; i--) printf flip[i] (i!=1?".":" ");
print $0;
}' infile
- define
@
as field delimiter with-F'@'
- split the second field on dot
.
separator to an array calledflip
- loop over elements of array from last to first and printf each and print back
.
except for first element; then print whole line$0
.
note: for the awk
that doesn't support array_length ( see AWK - How to count stored or index on array), try below instead that is first finding how many elements the array taken and use it as max in for-loop, like:
awk -F'@' '
{ split($2, flip, ".");
max=i=0; for (elements in flip) max++;
for (i=max; i>=1; i--) printf flip[i] (i!=1?".":" ");
print $0;
}' infile
You can do this perl if you insist on doing this in one line. Basically the -F flag is the same as awk so it splits each line on the @
character. The first part of the one liner creates a variable named $s
that has the reversed part of the domain. The second part of the one liner prints out the reversed domain followed by the original input that is stored in the $_
variable.
perl -F'@ ' -lane '$s = join ".", reverse split/\./, $F[-1]; print "$s $_"'
#sed -r -n 's/^([^@]+@)(.+)\.([a-z]{2,3})[\r\n\t ]{0,}$/\3.\2 \1\2.\3/gip' <<< "email: [email protected]"
or
#sed -r -n 's/^([^@]+@)(.+)\.([a-z]{2,3})[\r\n\t ]{0,}$/\3.\2 \1\2.\3/gip' ./your file
upd:FIXED TO SUPPORT A THIRD DOMAIN
sed -r -n 's/^([^@]+@)([^\.]+)(\.[^\.]+){0,1}\.([a-z]{2,3})[\r\n\t ]{0,}$/\4\3.\2 \1\2\3.\4/gip' <<< "email: [email protected]"
result: ru.yandex email: [email protected]
and
sed -r -n 's/^([^@]+@)([^\.]+)(\.[^\.]+){0,1}\.([a-z]{2,3})[\r\n\t ]{0,}$/\4\3.\2 \1\2\3.\4/gip' <<< "email: [email protected]"
result: com.changeip.josephay905s email: [email protected]
Thanks for the comment @TERDON