473,396 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

finding and replacing lines in a file

6
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

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 2115
richee
6
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 Expert 2GB
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
Nov 23 '07 #3
richee
6
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
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 Expert 2GB
I will take a look later tonight when I am off work.
Nov 26 '07 #6
KevinADC
4,059 Expert 2GB
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
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 Expert 2GB
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
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
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...
13
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...
1
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
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,...
32
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...
10
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...
7
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...
1
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...
3
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
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
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...
0
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,...
0
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...

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.