How to make sed remove everything else except an email address.

how do I get "sed" to delete everything else except an email address.

db dump: someusername ::: kRW...0fPc ::: $2a$10$...aucvkDt86 ::: joesmith@gmail.com
+3
source share
4 answers

Is there any need to do this? How about grep? Here's how to use it with the regex that you specified:

$ cat dbdump.txt 
db dump: someusername ::: kRW...0fPc ::: $2a$10$...aucvkDt86 ::: joesmith@gmail.com
another line with two e-mail addresses <test@example.com> on it -- bob@example.org

$ grep -EiEio '\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b' dbdump.txt
joesmith@gmail.com
test@example.com
bob@example.org

The flag -oprints only the relevant parts, i.e. email addresses only. -imakes a matching case insensitive. It even finds multiple email addresses on one line.

Edit: I could not resist -EiEio. I believe that grep -Eio, or egrep -iowill work too ...

+19
source

sed:

$ echo "db dump: someusername ::: kRW...0fPc ::: $2a$10$...aucvkDt86 ::: joesmith@gmail.com"|sed 's/.*::: //' joesmith@gmail.com 

awk:

$ echo "db dump: someusername ::: kRW...0fPc ::: $2a$10$...aucvkDt86 ::: joesmith@gmail.com"|awk '{print $NF}'

EDIT: - , , - . :

, , , 1$%3{C}@example.com - ( ). ( John Doe@example.com). , , .

, , , , @, :

cat your-file.txt|grep @

. - :

$ echo "garbage John.Doe123@example.com garbage"|sed 's/[^@]* \([a-zA-Z0-9.]*@[^ ]*\).*/\1/'
John.Doe123@example.com

, :

  • .
  • ( , )
  • local-part ( @) ( ),

([a-zA-Z0-9.]), - . [a-zA-Z0-9.-_], - _.

0

, , . , , . , .

sed 's/^.* \([^@ ]\+@[^ ]\+\) \?.*$/\1/'

$ cat dbdump
this line with no valid @ email address is untouched
::: a0$...aucvkDt86 ::: joesmith@gmail.com
::: a0$... foo@example.com db dump: someusername :::

$  sed 's/^.* \([^@ ]\+@[^ ]\+\) \?.*$/\1/' ./dbdump
this line with no valid @ email address is untouched
joesmith@gmail.com
foo@example.com
0

GNU sed:

sed -r 's/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/\n&\n/ig;s/(^|\n)[^@]*(\n|$)/\n/g;s/^\n|\n$//g;/^$/d' inputfile
  • separate input lines so that email addresses and other lines are separated by newlines
  • erase sequences that consist only of non-characters separated by newlines or the beginning or end of an input line
  • erase extra lines and empty lines
0
source

Source: https://habr.com/ru/post/1780134/


All Articles