I have a file that looks like this:

abuse@verizon.net abuse@verizon.net
abuse@verizon.net abuse@verizon.net
abuse@verizon.net abuse@verizon.net
abuse@universal.net.id abuse@universal.net.id abuse@universal.net.id
abuse@cnc-noc.net abuse@cnc-noc.net abuse@cnc-noc.net

I'd like to use sed (or awk) to manipulate the file so that everything after the first space and up to (but not including the space before) the first occurrence of a number is removed. So the above would end up looking like this:


I've tried:

sed  '/\s.*$\([0-9]\)/\1/g' <filename>
sed  '/\s.*$/,/0-9/p' <filename>
sed  '/\s.*$/,/\(\[0-9\]\)/p' <filename>

and many other combinations of examples I've found on the Web. I think the problem may be that sometimes the line is already as I want it; other times there are two or more extra email addresses between the first email address and the ip address.

Edit: I will always want the first email address occurring on a line (sometimes there is only one; other times there are more than one), and everything else on the line removed up until the space before the IP address at the end. I need to keep a space between the email address and the IP address. If the line contains only one email address and the IP address, I'd like to leave it alone.

Can anyone (please) help?

  • (Thank you for fixing my post Byte Commander...this is the first time I've used this.) – samgoober Dec 7 '15 at 16:20
  • 1
    Please edit your question and clarify. You say you don't want to include numbers but your desired output shows numbers as well. Also, will your files always be like that? Will you always want the 2nd and last fields? – terdon Dec 7 '15 at 16:30
  • Also, can we just say that you want the last occurence of an email address followed by an IP on each line? Anything wrong with this wording? – kos Dec 7 '15 at 16:32
  • kos, that would work as well. – samgoober Dec 7 '15 at 16:37
  • terdon, I misworded my original question. Thank you for catching that. I've fixed it and hopefully clarified what I'd like to do. The files will always be in this format, where there is one or more email addresses on a line, followed by a single IP address. – samgoober Dec 7 '15 at 16:39

To get the output you show (and not the one you describe), you can simply print the 1st and the last fields using awk:

awk '{print $1,$NF}' file

Or perl:

perl -lane 'print "$F[0] $F[$#F]"' file
  • Thank you terdon. Will this also work if there are three or four occurrences of the email address before the IP address? And will it retain the ones that are already properly formatted? Thanks, Sam – samgoober Dec 7 '15 at 16:43
  • Actually I answered my own question... This seems to work. Thank you so much for your help! – samgoober Dec 7 '15 at 16:44
  • @samgoober Yes, it will work for an arbitrary number of email addresses as long as i) the 2nd field is the email you want and ii) the last field is the IP you want. – terdon Dec 7 '15 at 16:45
  • I was wondering how to accept the solution. I worked on this for about three hours, and you answered it in about three minutes. I'm very grateful to you for your help. (I did change it to where it pulls the first email address since there will always be at least one but not always two, but wouldn't have gotten that far without your help.) Thank you again! – samgoober Dec 7 '15 at 16:46
  • @kos eek! You're right, I missed that and so did the OP, probably. Thanks, fixed. – terdon Dec 7 '15 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.