2

I use an e-mail quote attribution string like this:

On dd mmm yyyy hh:mm +hhmm, from info@example.org (Example Dot Org):

where info@example.org is the sender's e-mail address and Example Dot Org is the sender's real name.

When no real name is provided (or even sometimes when one is), the two are identical, and I end up with an attribution string like this:

On dd mmm yyyy hh:mm +hhmm, from info@example.org (info@example.org):

If (and only if) this is the case, I want to remove the (info@example.org) part, leaving me with only:

On dd mmm yyyy hh:mm +hhmm, from info@example.org:

I already have a shell script which calls out to Perl to massage the e-mail before feeding it to my editor, and am thinking I could simply add some code to that to massage the attribution line as well. The solution could be done in Perl, awk, sed, or something else (though preferably not something too exotic).

I do know that the attribution line is the first non-blank line following the first blank line of the input, and I don't mind hardcoding against my preferred attribution line format. Simply deleting the part in paranthesises is trivial, but how to do it only when it is equal to what precedes it? Preferably without risking altering anything else in the email (text file).

3

Just substitute the substring containing the two occurrences of the address with the substring's first half, containing only one address:

bash-4.2$ echo '
On dd mmm yyyy hh:mm +hhmm, from info@example.org (Example Dot Org):
On dd mmm yyyy hh:mm +hhmm, from info@example.org (info@example.org):
On dd mmm yyyy hh:mm +hhmm, from info@example.org:
' | sed -r 's/(, from (\S+)) \(\2\)/\1/'

On dd mmm yyyy hh:mm +hhmm, from info@example.org (Example Dot Org):
On dd mmm yyyy hh:mm +hhmm, from info@example.org:
On dd mmm yyyy hh:mm +hhmm, from info@example.org:
  • I ended up refining this to sed -r 's/^(On [0-3][0-9] [A-Z][a-z][a-z] [12][0-9]{3} [0-9][0-9]:[0-9][0-9] [+-][0-9]{4})(, from (\S+)) \(\3\):$/\1\2:/', which is a fair bit more restrictive in what it matches, but also more closely matches my usage. Your example was great as a starting point. Many thanks, and an upvote for posterity. :) – a CVn Apr 26 '13 at 20:23
0
perl -lane 'print "($F[07]):" eq "$F[8]" ? "@F[0..7]:" : "@F"'

This Perl one liner will split the string on spaces, so following string will be split into 9 parts:

On dd mmm yyyy hh:mm +hhmm, from info@example.org (info@example.org):

It will compare part8 (e-mail address) with part9 (senders name) and if they are same then part8 will be removed. leaving you with:

On dd mmm yyyy hh:mm +hhmm, from info@example.org:

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.