LinuxQuestions.org
Support LQ: Use code LQCO20 and save 20% on CrossOver Office
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 09-08-2008, 12:57 AM   #1
Ole Juul
Member
 
Registered: Sep 2008
Location: Coalmont, Canada
Distribution: Kubuntu
Posts: 48
Blog Entries: 1

Rep: Reputation: 15
Removing e-mail header in text file


I've been trying to remove the unneeded part of the header in stored e-mails so that they are easier to read. In DOS I can use rmheader.exe which will leave just a few parts such as "Subject:", "To:", "From:", "Date:", or whichever I specify. I am unable to find such a utility for linux. I tried sed, but that is very difficult for me and perhaps it is a bad approach for an amateur. (I'm still dizzy from that one!)

Can someone suggest a utility or script which I can use?

The e-mail files are in the form of sequential hex numbers eg "00001a.eml" but obviously i could change that if there was any advantage.
 
Old 09-08-2008, 04:36 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu (x86), Debian (PPC)
Posts: 3,528

Rep: Reputation: 60
A little perl one-liner can do it with some ease:
Code:
perl -ne 'if (/^\s*$/) {$b=1;} print if (/^(Subject|From|To|Date|Cc|Bcc):/||$b);' message.file
This will print the modified message on standard output. You can re-direct that to a new file as you like, or if you prefer to modify the original file in-place like this:
Code:
perl -i -ne 'if (/^\s*$/) {$b=1;} print if (/^(Subject|From|To|Date|Cc|Bcc):/||$b);' message.file
Of course you don't need to type all that in every time. You can put it in a file, make that executable and use it like any other program:
Code:
#!/usr/bin/perl -i -n
if (/^\s*$/) {$b=1;} 
print if (/^(Subject|From|To|Date|Cc|Bcc):/||$b);
Remove the -i from the first line if you don't want it to modify files in place.
 
Old 09-08-2008, 02:41 PM   #3
Ole Juul
Member
 
Registered: Sep 2008
Location: Coalmont, Canada
Distribution: Kubuntu
Posts: 48
Blog Entries: 1

Original Poster
Rep: Reputation: 15
Thankyou matthew4g,

I just tried that and it worked like a charm!

I don't know anything about perl, but now I have a little start to fool with in a useful way. This btw was my first post and boy am I pleased! Thanks again for going to the trouble of doing that script.
 
Old 09-08-2008, 09:51 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.2, Centos 5.8
Posts: 11,734

Rep: Reputation: 902Reputation: 902Reputation: 902Reputation: 902Reputation: 902Reputation: 902Reputation: 902Reputation: 902
If you are going to start learning Perl I recommend you bookmark these:

http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials .

Enjoy
 
Old 09-11-2008, 10:17 PM   #5
Berhanie
Senior Member
 
Registered: Dec 2003
Distribution: Fedora
Posts: 1,492

Rep: Reputation: 144Reputation: 144
There's also a thing called formail, which is useful for manipulating mail headers and messages. I think it's part of procmail.
 
Old 09-11-2008, 11:36 PM   #6
trickykid
Guru
 
Registered: Jan 2001
Posts: 24,121

Rep: Reputation: 121Reputation: 121
If you wanted to know the Bash or shell alternative, you can use grep to strip out some of the headers.

Something like this would work:

Code:
grep -v '^Subject' message.file | grep -v '^To:' | grep -v '^Bcc'
You could easily write a script to take command line arguments and or experiment with better ways to remove the fields you want, etc.
 
Old 09-12-2008, 12:00 AM   #7
trickykid
Guru
 
Registered: Jan 2001
Posts: 24,121

Rep: Reputation: 121Reputation: 121
Actually, an even easier way with grep is to use egrep like this so you don't have to pipe to grep -v for each word you want to remove:

Code:
egrep -v '^Subject:|^Bcc:|^To:' message.file
 
Old 09-12-2008, 01:22 AM   #8
Ole Juul
Member
 
Registered: Sep 2008
Location: Coalmont, Canada
Distribution: Kubuntu
Posts: 48
Blog Entries: 1

Original Poster
Rep: Reputation: 15
I just installed procmail to see if it has formail in the lib - it does. Actually procmail looks like a pretty useful package. Thanks for the tip Berhanie!

As for trickykid's suggestion of using grep and egrep, I had no idea that those utilities could be used in that fashion. I use a watered down (but extremely useful) version of those in DOS and they don't do anything like that. Jeesh! If you guys come up with any more suggestions, I won't get any work done ... :(
 
Old 03-11-2009, 04:28 AM   #9
deibertine
Member
 
Registered: Mar 2009
Posts: 215

Rep: Reputation: 30
Red face Extract Email headers in text file

How about extracting email headers' IP and Domain values all dumped in a text file, how do I write the code?

I have tons of email messages that I need to extract and only need to know their IPs and domain names were found.



Quote:
Originally Posted by matthewg42 View Post
A little perl one-liner can do it with some ease:
Code:
perl -ne 'if (/^\s*$/) {$b=1;} print if (/^(Subject|From|To|Date|Cc|Bcc):/||$b);' message.file
This will print the modified message on standard output. You can re-direct that to a new file as you like, or if you prefer to modify the original file in-place like this:
Code:
perl -i -ne 'if (/^\s*$/) {$b=1;} print if (/^(Subject|From|To|Date|Cc|Bcc):/||$b);' message.file
Of course you don't need to type all that in every time. You can put it in a file, make that executable and use it like any other program:
Code:
#!/usr/bin/perl -i -n
if (/^\s*$/) {$b=1;} 
print if (/^(Subject|From|To|Date|Cc|Bcc):/||$b);
Remove the -i from the first line if you don't want it to modify files in place.
 
  


Reply

Tags
headers


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C, removing header of a binary file and then downloading bket Linux - Newbie 1 01-24-2008 04:54 PM
removing blank lines in a text file christianunix Linux - Newbie 11 10-29-2007 12:24 AM
help removing some text from a file (sed) BrianK Programming 2 08-13-2006 07:10 PM
removing text shadows in kde; removing taskbar color gradient webazoid Linux - Software 1 04-11-2006 02:56 AM
I want change mail header info, which config file i should modify? exper Linux - Software 2 12-05-2003 06:46 AM


All times are GMT -5. The time now is 07:53 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration