Reputation: 171
How to sandwich only an e-mail address field with brackets in my address book? My address book consists of name, surname, nickname, mailing address (a csv format). I must add brackets only to the addresses to comfort with the format of address book of my new mailer (MUA). I would like to know how to implement this with sed/awk. The MWE is as follows:
John,Doe,JohnDoe,[email protected]
The converted result is desired to be like this:
JohnDoe John Doe <[email protected]>
Should I depend on the fact that only addresses include the at-mark? In this case, if the other fields include this mark, errors might occur resultingly. Thanks in advance for your advises.
Upvotes: 0
Views: 108
Reputation: 84579
With your addition of .csv content of:
John,Doe,JohnDoe,[email protected]
Where you need to enclose the "[email protected]"
within <...>
, you can simply do:
awk -F, -v OFS=, '{$4="<"$4">"}1' file
Which will then modify each record in the file adding "<"
and ">"
at the end of the 4th field before using the default print command shorthand 1
at the end to output the record. Setting the variable OFS
(Output Field Separator) to ','
ensures each output field is separated by a comma.
Example Use/Output
$ echo "John,Doe,JohnDoe,[email protected]" | awk -F, -v OFS=, '{$4="<"$4">"}1'
John,Doe,JohnDoe,<[email protected]>
Output Without ','
Separators
Since you also show wanted output without any .csv comma separators, you can simply not alter OFS
and that will produce space-separated output, e.g.
$ echo "John,Doe,JohnDoe,[email protected]" | awk -F, '{$4="<"$4">"}1'
John Doe JohnDoe <[email protected]>
Rearranging Field Order To Match Your Wanted Output
As @EdMorton caught, you are swapping the first couple of fields as well. To accomplish the field reorder and cap the e-mail address, you can do:
awk -F, '{print $3, $1, $2, "<"$4">"}' file
Example Rearranged
$ echo "John,Doe,JohnDoe,[email protected]" | awk -F, '{print $3, $1, $2, "<"$4">"}'
JohnDoe John Doe <[email protected]>
There are several way to approach this issue, but using awk
with this, or something similar, is likely to be among the most efficient.
Let me know if you have further questions.
Upvotes: 2