Hi all,
I am new to Perl but see that it offers some fantastic opportunities and am trying to use it more in problem solving but could do with a little help on a problem thats driving me nuts....
I am trying to remove certain sub headers from email, i want it to tick through the text until it reaches "content-disposition:" then delete the next two lines.
The ticking through part is no problem but i cant seem to do anything to the file once i get there.
So, in a nutshell every time it sees:
Content-Disposition: attachment; filename="attachment.pdf"; size=0;
creation-date="Thu, 12 Apr 2007 06:39:10 GMT";
modification-date="Thu, 12 Apr 2007 06:39:10 GMT"
Content-Transfer-Encoding: base64
it should change it to
Content-Disposition: attachment; filename="attachment.pdf";
Content-Transfer-Encoding: base64
the code i have is -
-
use strict;
-
use warnings;
-
-
my $line_count = 0;
-
my $dispo_count = 0;
-
my $file = 'bad.MAI';
-
open(FILE, $file); #opens file
-
open(PROC_FILE, ">proc_file.txt");#file to write data to check its working
-
-
while (<FILE>) {
-
-
if ( $_ =~ /^\Content-Disposition:/ ) {
-
$dispo_count ++; # this is just for information
-
-
my $nextline1 = <FILE> ;
-
my $nextline2 = <FILE> ;
-
print PROC_FILE ("$_ $nextline1 $nextline2" ); #this is only to check the thing is finding the right place
-
-
#how the hell do i delete or replace with nothihg the 2 found lines??
-
}
-
$line_count ++;
-
-
}
-
print PROC_FILE "line count = $line_count\n"; # these are just for information
-
print PROC_FILE "dispo count = $dispo_count\n"; # these are just for information
-
close(FILE);
-
close(PROC_FILE);
-
I was trying to use something like:
$nextline1 =~ tr/.*//;
to replace the line with nothing but i cant seem to write the the file at all.
Does anyone have any ideas what would be the best way to approach this?
Thanks a lot!
Richee.
9 2115
Hi again,
I hope nobody wasted any time time on this tomfoolery of mine. I tried a different tack and got much closer with this: -
use strict;
-
use warnings;
-
-
my $line_count = 0;
-
my $dispo_count = 0;
-
my $file = 'bad.MAI';
-
open(FILE, $file); #opens file
-
open(PROC_FILE, ">proc_file.txt");#file to write data to check its working
-
my @file = <FILE>;
-
-
foreach my $line (@array_of_data)
-
{
-
if ( $line =~ /^\Content-Disposition:/ ) {
-
print PROC_FILE "$line\n";
-
$dispo_count ++;
-
$line =~ s/Size=.*;//gi;
-
print PROC_FILE "$line\n";
-
}
-
$line_count ++;
-
}
-
-
-
-
print PROC_FILE "line count = $line_count\n";
-
print PROC_FILE "dispo count = $dispo_count\n";
-
-
-
open DATAOUT, ">$file" or die "can't open $file $!";
-
-
-
foreach my $line (@file)
-
{
-
-
print DATAOUT "$line";
-
}
-
-
close (DATAOUT);
-
-
-
close(FILE);
-
close(PROC_FILE);
so now i have to just replace the next 2 lines.
Cheers,
Richee
is this still what you are trying to accomplish?
So, in a nutshell every time it sees:
Content-Disposition: attachment; filename="attachment.pdf"; size=0;
creation-date="Thu, 12 Apr 2007 06:39:10 GMT";
modification-date="Thu, 12 Apr 2007 06:39:10 GMT"
Content-Transfer-Encoding: base64
it should change it to
Content-Disposition: attachment; filename="attachment.pdf";
Content-Transfer-Encoding: base64
Hi Kevin,
Thanks for taking the time to reply, yes I am still trying to accomplish this.
I just about have it, but its so clumsy i am embarrassed to post it. I couldn't figure out how to find the search string (so Content-Disposition in this case) then delete the next two lines, then carry on searching so i had to resort to ifs for the text i wanted to delete but aside from this being rubbish it also leaves 2 blank lines in the text which doesn't really achieve what i'm trying to do.
Any suggestions?
here is what i have so far: - my $path = "C:\\Program Files\\etc";
-
-
-
open(FILE, $path);
-
-
my @array = <FILE>;
-
-
foreach my $line (@array)
-
-
-
{
-
if ( $line =~ /^\Content-Disposition:/ ) {
-
$line =~ s/Size=.*;//gi;
-
-
-
}
-
if ( $line =~ /creation-date=/ ) {
-
$line =~ s/.*//gi;
-
}
-
-
if ( $line =~ /modification-date=/ ) {
-
$line =~ s/.*//gi;
-
}
-
-
-
}
-
-
open DATAOUT, ">$path" or die "can't open $path $!";
-
-
-
foreach my $line (@array)
-
{
-
-
print DATAOUT "$line";
-
}
-
close (DATAOUT);
-
close(FILE);
-
thanks a lot,
Richee
Hi again,
At the risk of further embarrassment i have found some kind of way of doing what i was trying to acheive, albeit rather crudely. - open(FILE, $path);
-
-
my @array = <FILE>;
-
my $i= 0;
-
foreach my $line (@array)
-
-
{
-
if ( $line =~ /^\Content-Disposition:/ ) {
-
$line =~ s/Size=.*;//gi;
-
splice(@array, $i+1,2)
-
-
}
-
-
-
$i++;
-
-
}
-
-
open DATAOUT, ">$path" or die "can't open $path $!";
-
-
-
foreach my $line (@array)
-
{
-
-
print DATAOUT "$line";
-
}
-
close (DATAOUT);
-
close(FILE);
I guess it could be done much better and would be interested to hear how.
Cheers,
Richee.
I will take a look later tonight when I am off work.
Assuming that you know you will always skip two lines follow Content-Disposition: - use strict;
-
use warnings;
-
my $path = 'path/to/file';
-
-
{
-
local @ARGV = ($path);
-
local $^I = '.bak'; # use inplace editing
-
while ( <> ) {
-
if (/^\Content-Disposition:/ ) {
-
print;
-
readline <>;
-
readline <>;
-
}
-
else {
-
print;
-
}
-
}
-
}
-
print 'finished';
Thanks, great stuff Kevin. Interested to see the inplace editing, practically everything i had read said it wasnt possible to manipulate files live in this way.
Regarding the readline command and the $line array variable i was using, do you happen to know if its possible to compare two elements of the array using these, so for example if $line = $line +3, or if readline = readline +3 to compare the current element with current element +3 lines. This would be really useful to know but i couldnt seem to get it to work.
Thanks again,
Richee.
Thanks, great stuff Kevin. Interested to see the inplace editing, practically everything i had read said it wasnt possible to manipulate files live in this way.
Regarding the readline command and the $line array variable i was using, do you happen to know if its possible to compare two elements of the array using these, so for example if $line = $line +3, or if readline = readline +3 to compare the current element with current element +3 lines. This would be really useful to know but i couldnt seem to get it to work.
Thanks again,
Richee.
readline does not work with arrays, only filehandles. You can compare array elements using the array index:
if ($array[n] eq $array[n+3])
where "n" is the current index number and n+3 is three elements after n.
Fantastic, just the ticket.
Thanks,
Richee.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: lecichy |
last post by:
Hello
Heres the situation:
I got a file with lines like:
name:second_name:somenumber:otherinfo
etc with different values between colons ( just like passwd file)
What I want is to extract...
|
by: yaipa |
last post by:
What would be the common sense way of finding a binary pattern in a
..bin file, say some 200 bytes, and replacing it with an updated pattern
of the same length at the same offset?
Also, the...
|
by: Andrew Poulos |
last post by:
Say I have some CSS, which is several hundred lines long, with the
contents in this format:
..foo {
blah
color:#000;
blah
}
..bar {
blah
|
by: e271828 |
last post by:
Hi,
I'm helping to work a developer tool that verifies a given HTML element
has a given attribute (e.g., that all LABEL elements have a FOR
attribute, all INPUT elements have an ID attribute,...
|
by: FireHead |
last post by:
Hello C World & Fanatics
I am trying replace fgets and provide a equavivalant function of
BufferedInputReader::readLine.
I am calling this readLine function as get_Stream.
In the line 4 where...
|
by: blazedaces |
last post by:
Alright guys, so the title explains exactly my goal. The truth is I'm going to be reading in a lot of data from an xml file. The file is too large and there's too much data to store in arraylists...
|
by: DarthBob88 |
last post by:
I have to go through a file and replace any occurrences of a given
string with the desired string, like replacing "bug" with "feature".
This is made more complicated by the fact that I have to do...
|
by: Matt Herzog |
last post by:
Hey All.
I'm learning some python with the seemingly simple task of updating a firewall config file with the new IP address when my dhcpd server hands one out. Yeah, I know it's dangerous to edit...
|
by: nicolin |
last post by:
Hello!
I`m trying to replace only one line in txt file!
Example!
test.txt file
AAA = 1
AAA = 2
AAA = 3
BBB = 2
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |