sed help plz
I am looking to use sed in a script. I have searched through the docs and cannot find what I am looking for.
If I have an email address like frustrated_sed_user@smtp1.mail.linux.com. How do I get the topmost domain out? (ie linux.com) RK |
awk is a better tool for extracting fields:
The "-F." is telling awk to use dot as the field separator instead of white space. The dot in quotes in the print section is adding the dot back to the output as awk stripped it out when it broke the fields up. Fields 3 and 4 are being printed before and after the dot respectively. |
Well, I can tell I'm warmer...
How do I do a reverse search? If I do: Code:
echo frustrated_sed_user@smtp1.mail.linux.com |awk -F. '{print $3"."$4}' Code:
echo frustrated_sed_user@smtp1.linux.com |awk -F. '{print $3"."$4}' Further clarification: I'm looking for a more general email address filter, not something that only works with that one string. Oh and thank you very much for your reply. I think awk may be what I need, but geez that's a lot of documentation. RK |
Code:
echo frustrated_sed_user@smtp1.mail.linux.com |awk -F. '{print $(NF-1)"."$NF}' |
Really good tutorials here---SED, AWK, and more:
http://www.grymoire.com/Unix/ Beats reading man pages.....;) |
Being not well versed in awk, I only use it when data is (extremely) well structured - as in the cases above.
Must admit I have a leaning toward regex in that it can be used to extract data from anywhere in a record - building a (fool-proof) regex for this could get challenging though. Perl might be a better option than sed. |
Another method
Code:
echo ' |
Quote:
Code:
bash-3.1$ echo frustrated_sed_user@smtp1.mail.linux.com | rev | cut -d . -f 1 |
Quote:
|
Quote:
|
Quote:
Code:
bash-3.1$ echo frustrated_sed_user@smtp1.mail.linux.com | rev | awk -F. '{print $1"."$2}'| rev Code:
bash-3.1$ echo frustrated_sed_user@smtp1.mail.linux.com | awk -F. '{print $(NF-1)"."$NF}' NF is the number of fields, so NF is the last field, and NF-1 is the next to last field. Whichever way you want to do it, there are so many ways. |
Quote:
Code:
# time echo frustrated_sed_user@smtp1.mail.linux.com |awk -F. '{print $(NF-1)"."$NF}' |
Oh yeah, I guess I missed that post, and posted the same thing just a second ago. Oh whatever, 0.003 sec is that important.
|
Quote:
|
My second post did it without having to "rev" anything. I just changed the variables to be relative to number of fields rather than explicit 3 and 4.
|
All times are GMT -5. The time now is 12:17 AM. |