473,770 Members | 3,710 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

finding and replacing lines in a file

6 New Member
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. use strict;
  3. use warnings;
  4.  
  5. my $line_count = 0;
  6. my $dispo_count = 0;
  7. my $file = 'bad.MAI'; 
  8. open(FILE, $file); #opens file 
  9. open(PROC_FILE, ">proc_file.txt");#file to write data to check its working
  10.  
  11. while (<FILE>) {
  12.  
  13. if ( $_ =~ /^\Content-Disposition:/ ) {    
  14.     $dispo_count ++; # this is just for information
  15.  
  16.       my $nextline1 = <FILE> ;
  17.       my $nextline2 = <FILE> ;
  18.                 print PROC_FILE ("$_ $nextline1 $nextline2" ); #this is only to check the thing is finding the right place
  19.  
  20.                #how the hell do i delete or replace with nothihg the 2 found lines??
  21. }
  22. $line_count ++;
  23.  
  24. }
  25. print PROC_FILE "line count = $line_count\n"; # these are just for information
  26. print PROC_FILE "dispo count = $dispo_count\n"; # these are just for information
  27. close(FILE);
  28. close(PROC_FILE);
  29.  
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.
Nov 23 '07 #1
9 2152
richee
6 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. my $line_count = 0;
  5. my $dispo_count = 0;
  6. my $file = 'bad.MAI'; 
  7. open(FILE, $file); #opens file 
  8. open(PROC_FILE, ">proc_file.txt");#file to write data to check its working
  9. my @file = <FILE>;
  10.  
  11. foreach my $line (@array_of_data)
  12. {
  13.  if ( $line =~ /^\Content-Disposition:/ ) {
  14.      print PROC_FILE "$line\n";
  15.      $dispo_count ++;
  16.      $line =~ s/Size=.*;//gi;
  17.      print PROC_FILE "$line\n";
  18.  }  
  19. $line_count ++;     
  20.  
  21.  
  22.  
  23. print PROC_FILE "line count = $line_count\n"; 
  24. print PROC_FILE "dispo count = $dispo_count\n";
  25.  
  26.  
  27. open DATAOUT, ">$file" or die "can't open $file $!";
  28.  
  29.  
  30. foreach my $line (@file)
  31.    {
  32.  
  33.    print DATAOUT "$line";
  34.    }
  35.  
  36. close (DATAOUT);
  37.  
  38.  
  39. close(FILE);
  40. close(PROC_FILE);
so now i have to just replace the next 2 lines.

Cheers,

Richee
Nov 23 '07 #2
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
Nov 23 '07 #3
richee
6 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. my $path = "C:\\Program Files\\etc";
  2.  
  3.  
  4. open(FILE, $path);
  5.  
  6. my @array = <FILE>;
  7.  
  8. foreach my $line (@array)
  9.  
  10.  
  11. {
  12.  if ( $line =~ /^\Content-Disposition:/ ) {
  13.      $line =~ s/Size=.*;//gi; 
  14.  
  15.  
  16.  }  
  17.  if ( $line =~ /creation-date=/ ) {
  18.      $line =~ s/.*//gi;     
  19.  } 
  20.  
  21.  if ( $line =~ /modification-date=/ ) {
  22.      $line =~ s/.*//gi;     
  23.  }   
  24.  
  25.  
  26.  
  27. open DATAOUT, ">$path" or die "can't open $path $!";
  28.  
  29.  
  30. foreach my $line (@array)
  31.    {
  32.  
  33.    print DATAOUT "$line";
  34.    }
  35. close (DATAOUT);
  36. close(FILE);
  37.  
thanks a lot,

Richee
Nov 26 '07 #4
richee
6 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. open(FILE, $path);
  2.  
  3. my @array = <FILE>;
  4. my $i= 0;
  5. foreach my $line (@array)
  6.  
  7. {
  8.  if ( $line =~ /^\Content-Disposition:/ ) {
  9.      $line =~ s/Size=.*;//gi; 
  10.      splice(@array, $i+1,2)    
  11.  
  12.  }  
  13.  
  14.  
  15.  $i++;
  16.  
  17. }
  18.  
  19. open DATAOUT, ">$path" or die "can't open $path $!";
  20.  
  21.  
  22. foreach my $line (@array)
  23.    {
  24.  
  25.    print DATAOUT "$line";
  26.    }
  27. close (DATAOUT);
  28. close(FILE);
I guess it could be done much better and would be interested to hear how.

Cheers,


Richee.
Nov 26 '07 #5
KevinADC
4,059 Recognized Expert Specialist
I will take a look later tonight when I am off work.
Nov 26 '07 #6
KevinADC
4,059 Recognized Expert Specialist
Assuming that you know you will always skip two lines follow Content-Disposition:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. my $path = 'path/to/file';
  4.  
  5. {
  6.    local @ARGV = ($path);
  7.    local $^I = '.bak'; # use inplace editing 
  8.    while ( <> ) {
  9.       if (/^\Content-Disposition:/ ) {
  10.          print;
  11.          readline <>;
  12.          readline <>;
  13.       }
  14.       else {
  15.          print;
  16.       }
  17.    }
  18. }
  19. print 'finished';
Nov 27 '07 #7
richee
6 New Member
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.
Nov 27 '07 #8
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.
Nov 27 '07 #9
richee
6 New Member
Fantastic, just the ticket.

Thanks,

Richee.
Nov 29 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
6955
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
13
15257
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...
1
1238
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
12
3408
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...
32
3899
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;
10
15096
blazedaces
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...
7
4910
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...
1
1645
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...
3
1381
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
0
9432
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,...
0
10232
Oralloy
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...
0
10059
jinu1996
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...
1
10008
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,...
0
9873
tracyyun
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...
1
7420
isladogs
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...
0
6682
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();...
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2822
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.