View Full Version : awk print the body of an email
maddog21
June 17th, 2012, 03:20 AM
I have fetched a couple of emails and stored then in /fetch/mail/oracle.txt, the displayed content is
Date:
From:
Subject:
To:
Content-Type:
---body -----
---------
Date:
From:
Subject:
To:
Content-Type:
----body -----
------
Date:
From:
I want to retrieve the body only from the emails i fetched,the problem i am having is the body doesn't have a tag,
the output should be like
email-1
----body---
email-2
----body---
jobix
June 17th, 2012, 06:10 AM
If you know for sure that the only lines that you do not need are these:
Date:
From:
Subject:
To:
Content-type:
you could simply use "grep -v"
e.g., grep -v "^Date:" oracle.txt | grep -v "^From:" ......[etc., etc.] > final-output.txt
The ^ means the beginning of a line.
lisati
June 17th, 2012, 06:13 AM
Most emails have a header section followed by a blank line, followed by the body.
maddog21
June 17th, 2012, 07:32 AM
If you know for sure that the only lines that you do not need are these:
Date:
From:
Subject:
To:
Content-type:
you could simply use "grep -v"
e.g., grep -v "^Date:" oracle.txt | grep -v "^From:" ......[etc., etc.] > final-output.txt
The ^ means the beginning of a line.
grep -v "^Date" /var/mail/oracle | grep -v "^From:" /var/mail/oracle | grep -v "^To:" /var/mail/oracle |grep -v "^Subject:" /var/mail/oracle > final-output.csv
i tried the above command but it displays everything instead of displaying the body only.
Sorry for the inconvenience, Thank you.
codemaniac
June 17th, 2012, 07:49 AM
You can tell awk what is the start and end pattern .Something like below .
awk '/---body -----/,/------/{print}' email.txt
jobix
June 24th, 2012, 04:32 AM
grep -v "^Date" /var/mail/oracle | grep -v "^From:" /var/mail/oracle | grep -v "^To:" /var/mail/oracle |grep -v "^Subject:" /var/mail/oracle > final-output.csv
i tried the above command but it displays everything instead of displaying the body only.
Sorry for the inconvenience, Thank you.
I guess I should have been more explicit. You should not specify the file name after the first pipe.
grep -v some-pattern filename | grep -v another-pattern | grep -v yet-another-pattern > final-output
"-v" is to exclude. | is used to direct the output from the previous operation to the next command. So, you only need the filename in the first command, i.e., before the first |
codemaniac
June 24th, 2012, 05:30 AM
I guess I should have been more explicit. You should not specify the file name after the first pipe.
grep -v some-pattern filename | grep -v another-pattern | grep -v yet-another-pattern > final-output
"-v" is to exclude. | is used to direct the output from the previous operation to the next command. So, you only need the filename in the first command, i.e., before the first |
If you are using grep , you can avoid using multiple pipes and save some keystrokes .Use extended features , see below
egrep -v "(some-pattern filename|another-pattern|yet-another-pattern)" filename > final-output
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.