Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awking an email 1

Status
Not open for further replies.

BIS

Technical User
Jun 1, 2001
1,893
NL
Hallo All,

I am running an incoming email through an awk script, the idea being to rewrite the body by adding tags like

(original body): Name : John Doe
(rewritten): <Name>John Doe</Name>

The problem I am facing is that the awk script seems to mess up the headers.

Is there a way for awk to only start doing its thing after the first completely empty line?
 
Hmm, I get errors.
This is the awk script:

# Set field-separator.
BEGIN { FS = " *= *" }
# Remove trailing spaces.
{ sub(/[ \t]+$/, "") }
/^$/ {flg=1}
flg{
/^langue/ { pr(); $0 = "<preferred_lang>"$2"</preferred_lang>"
print;next}
/^city/ { pr(); $0 = "<city>"$2"</city>"
print; next}
# Grab the text of any remaining text.
/^textfield2/ { pr(); accum="<problem_desc>" $2; next}
NF { accum = accum RS $0 }
END { pr() }
function pr()
{ if ( accum ) print accum "</problem_desc>"
accum = ""
}
}


line 7: syntax error at or near {


Any ideas?
 
And this ?
# Set field-separator.
BEGIN { FS = " *= *" }
# Remove trailing spaces.
{ sub(/[ \t]+$/, "") }
/^$/ {flg=1}
flg!=1{next}
/^langue/ { pr(); $0 = "<preferred_lang>"$2"</preferred_lang>"
print;next}
/^city/ { pr(); $0 = "<city>"$2"</city>"
print; next}
# Grab the text of any remaining text.
/^textfield2/ { pr(); accum="<problem_desc>" $2; next}
NF { accum = accum RS $0 }
END { pr() }
function pr() {
if ( accum ) print accum "</problem_desc>"
accum = ""
}

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Yep - perfect. Thanks.
 
Another quick question, might seem incredibly silly but here goes.

The above script starts processing the email and adding the relevant tags. This is all fine. The output looks something like this:

<preferred_lang>en</preferred_lang>
<city>London</city>
<problem_desc>Some random text here.
And here.
And possibly here</problem_desc>


Is there a way to add some tags to the beginning and end of this, so that the result would look something like this:

<TAG1>
<TAG2>
<preferred_lang>en</preferred_lang>
<city>London</city>
<problem_desc>Some random text here.
And here.
And possibly here</problem_desc>
</TAG2>
</TAG1>

?
 
Why not write the <TAGi> in the BEGIN section and the </TAGj> in the END section ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Yes.... but how?

I guess I am missing the obvious here...
 
# Set field-separator.
BEGIN { FS = " *= *"[highlight]; print "<TAG1>\n<TAG2>"[/highlight] }
# Remove trailing spaces.
{ sub(/[ \t]+$/, "") }
/^$/ {flg=1}
flg!=1{next}
/^langue/ { pr(); $0 = "<preferred_lang>"$2"</preferred_lang>"
print;next}
/^city/ { pr(); $0 = "<city>"$2"</city>"
print; next}
# Grab the text of any remaining text.
/^textfield2/ { pr(); accum="<problem_desc>" $2; next}
NF { accum = accum RS $0 }
END { pr()[highlight]; print "</TAG2>\n</TAG1>"[/highlight] }
function pr() {
if ( accum ) print accum "</problem_desc>"
accum = ""
}

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
{hides his head in shame}
Doh!
... Thank you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top