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="attac hment.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="attac hment.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 2152
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
KevinADC 4,059
Recognized Expert Specialist
is this still what you are trying to accomplish?
So, in a nutshell every time it sees:
Content-Disposition: attachment; filename="attac hment.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="attac hment.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.
KevinADC 4,059
Recognized Expert Specialist
I will take a look later tonight when I am off work.
KevinADC 4,059
Recognized Expert Specialist
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.
KevinADC 4,059
Recognized Expert Specialist
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 some part of it like all names or numbers from
each line, simply text fom between e.g. second and third colon. And turn it
|
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 pattern can occur on any byte boundary in the file, so
chunking through the code at 16 bytes a frame maybe a problem. The
file itself isn't so large, maybe 32 kbytes is all and the need for
speed is not so great, but the need for accuracy in 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, etc.). Pretty much
all of the functionality is working except a feature that shows in
which line of the HTML source a violation of the user-set rule occurs
(e.g., where a LABEL element doesn't have a FOR attribute). There
doesn't seem to be a...
|
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 default_buffer_length is changed from 4 --24 code
works fine.
But on the same line if I change the value of default_buffer_length
from 4 --10 and I get a memory error.
And if the change the value of the same variable from 4 --1024 bytes;
| |
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 without running out of memory, so I'm reading and as I'm reading I'm going to write to a file.
This is the thing though, I already can do this and have it done, but I want to modify the program so you can choose what data you want to take out. To...
|
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 this with
a lot of replacements and by the fact that some of the target strings
are two words or more long, so I can't just break up the file at
whitespace, commas, and periods. How's the best way to do this? I've
thought about using strstr() to...
|
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 such a file "in place" but this is just an exercise at this point. I would not mind using file handles except they seem to add complexity.
The only apparent problem I have with my script so far is that it's adding lots of blank lines to the file...
|
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: 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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |