Parsing to_addr field in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing to_addr field in bash
# 1  
Old 07-25-2017
Parsing to_addr field in bash

Hi there,

I'm trying to parse the to_addr field of emails and split it into individual email addresses. The idea is to split using the comma character (,):
These two first approach work:
Code:
$ field="'Paul FOO' <paul@foo.com>, Andrew   FOO <andrew@foo.com>"
$ IFS=, read -r -a array <<< "$field"; for a in "${array[@]}"; do echo "$a"; done
'Paul FOO' <paul@foo.com>
 Andrew   FOO <andrew@foo.com>
$ (IFS=,; for a in $field; do echo "$a"; done)
'Paul FOO' <paul@foo.com>
 Andrew   FOO <andrew@foo.com>

But it fails if one name has a comma:
Code:
$ field='"Paul FOO" <paul@foo.com>, "Andrew, M. FOO" <andrew@foo.com>'
$ IFS=, read -r -a array <<< "$field"; for a in "${array[@]}"; do echo "$a"; done
"Paul FOO" <paul@foo.com>
 "Andrew
 M. FOO" <andrew@foo.com>
$ (IFS=,; for a in $field; do echo "$a"; done)
"Paul FOO" <paul@foo.com>
 "Andrew
 M. FOO" <andrew@foo.com>

How can I handle this situation?

Regards
Santiago
# 2  
Old 07-25-2017
Just thinking on this...

Seem to recall years ago a similar type of problem. So, I changed the trap rules.
If I am following your example, each record should end with .com> or .com>,
So, what if before your awk you substitute .com>, with .com>; and then use the ; as your ifs? Not sure if all are .com so you may need to substitute with .org also.
If you do not want to use the ; character, you could also try a character not likely to appear like ~ or ^ or |
# 3  
Old 07-25-2017
Thanks joeyg for your thoughts...
It's unfortunately more complicated.
On the small pool I'm working on (4000+ messages / 200- addresses), I already have a few patterns:
Code:
 
FOO
Raw
root
Support
undisclosed-recipients:
Undisclosed, recipients:
Santiago DIEZ <foo@bar.com>
"Santiago DIEZ" <foo@bar.com>
"Santiago, F. DIEZ" <foo@bar.com>
"'Santiago DIEZ' via Gmail" <foo@bar.com>
<foo@bar.com>
foo@bar.com
foo@bar.com (Santiago DIEZ)

Line 1 is blank. Lines 2-7 will be ignored so it's OK if we break line 7 (because of the comma). I already have a regex pattern for the rest but I fear to bump on some new patterns so I'd like a way of splitting by the comma (except when between quotes).
# 4  
Old 07-25-2017
You will have to write a parser in awk to handle unvalidated user input like that.

IF you have Linux - gawk should be there. Try regular expressions for field delimiting patterns:

Example:
Code:
gawk -vFPAT='[^,]*|"[^"]*"'   '{ your code to print goes here, split with FPAT }' somefile

You can use alternation:
Code:
 -vFPAT='(pattern set 1|pattern set 2|pattern set 3)'

You can also declare fields with
Code:
awk -F 'regex goes here' {code here}' somefile

I cannot give you a fixed set of rules to use, it looks like you do not have a complete set yet. You should do some serious validation on the input to that dataset so you do not get difficult formatting problems. Otherwise you may have to resort to using some bizarre character as a field delimiter. Maybe high ASCII > 127.
This User Gave Thanks to jim mcnamara For This Post:
# 5  
Old 07-26-2017
Great lead. Thanks jim mcnamara.

Last edited by chebarbudo; 07-26-2017 at 05:10 AM.. Reason: typo
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing out data with multiple field separators

I have a large file that I need to print certain sections out of. file.txt /alpha/beta/delta/gamma/425/590/USC00015420.blah.lt.0.01.str:USC00015420Y2017M10BLALT.01 12 13 14 -9 1 -9 -9 -9 -9 -9 1 2 3 4 5 -9 -9 I need to print the "USC00015420" and... (5 Replies)
Discussion started by: ncwxpanther
5 Replies

2. Shell Programming and Scripting

File Parsing based on a character in a specific field

Hi All, I'm having a hard time finding a starting point for my issue. I have a 30k line file (fspsec.txt) that I would like to parse into smaller files based on any character existing in field 1. ACCOUNTANT LEVEL 1 (ACCT.ACCOUNTANT) OPERATORS: DOEJO (418) TOOLS: Branch Maintenance ... (2 Replies)
Discussion started by: aahlrich
2 Replies

3. UNIX for Advanced & Expert Users

Parsing a file in bash

Hello All, I have the following input file that i'm trying to parse: 10.0.011.40 hadoop 15526 15524 0 hadoop 15528 15526 0 hadoop 19747 4018 1 10.0.081.227 hadoop 2862 2861 0 hadoop 2864 2862 0 hadoop 12177 14376 1 I'm trying to get this in my output file: 10.0.011.40 15526 15528... (2 Replies)
Discussion started by: ramky79
2 Replies

4. Shell Programming and Scripting

Column parsing in a bash script - HELP

I would like to setup a script that pulls in time/date in two seperate columns, and then name the other columns as listed below: Column1=Art/TJ output Column2=Art/TJ output Column3=TJ output column4=Art output Column5=If time/date past 12:00 noon -fail Colume6=If time/date before... (1 Reply)
Discussion started by: walnutpony123
1 Replies

5. Shell Programming and Scripting

BASH parsing for html tags

Hello can anyone help me parse this line. <tr><td>United States of America</td><td>Dollar</td><td>43.309</td></tr><tr><td>Japan</td><td>Yen</td><td>0.5579</td></tr> the line above did not break. so i would like to have a result like this United States of America Dollar 43.309 Japan... (3 Replies)
Discussion started by: doomsayer16
3 Replies

6. Shell Programming and Scripting

parsing a config file using bash

Hi , I have a config _file that has 3 columns (Id Name Value ) with many rows . In my bash script i want to be able to parse the file and do a mapping of any Id value so if i have Id of say brand1 then i can use the name (server5X) and Value (CCCC) and so on ... Id Name ... (2 Replies)
Discussion started by: nano2
2 Replies

7. Shell Programming and Scripting

Parsing (Bash)

Hello, I need help. I create www page, and I have link, where is weather and is updated each hour. And I need cut only weather from source code. Example: Monday : 12/14 ... Can you help me? Thanks (2 Replies)
Discussion started by: krcek12
2 Replies

8. Shell Programming and Scripting

Help parsing filename with bash script

Hi all! Looking for some help parsing filenames in bash. I have a directory full of files named "livingroom-110111105637.avi". The format is always date and time (yymmddhhmmss). I'm looking to parse the filenames so they are a little more easily readable. Maybe rename them to... (4 Replies)
Discussion started by: mtehonica
4 Replies

9. Programming

parsing fixed length field with yacc/bison

How to specify the token length in a yacc file? sample input format <field1,data type ans,fixed length 6> followed by <field2,data type ans,fixed length 3> Example i/p and o/p Sample Input: "ab* d2 9o" O/p : "Field1 Field2 " yacc/bison grammar: record :... (1 Reply)
Discussion started by: sungita
1 Replies

10. Shell Programming and Scripting

Parsing Directory Names for Use as Bash Variables

Hi all: I have a directory where all of the subdirectories are named by the convention "images_#1:#2_Date." My goal is to get an array for each subdirectory that has the structure (#1,#2, int). I am able to use awk to print each subdirectory's values, but cannot figure out how to get them into an... (6 Replies)
Discussion started by: aefskysa
6 Replies
Login or Register to Ask a Question