Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

does perl have a built in sed?

Status
Not open for further replies.

gammaman1

Programmer
Jun 30, 2005
23
US
hi,

I have a windows version of perl installed, and I realized that I need a sed function that's like the unix version. Does perl have a sed? if so, would you call it the same as you would do in unix? ie. " sed -e '/^ *$/d' input" ?

thanks,
gammaman
 
It appears to me that you're trying to lose blank lines.
To do that you could do this:
Code:
perl -lne 'print unless /^\s*$/'

Trojan.
 
so how would I remove blank lines from a file?

for example,

open FILENAME, "temp";
@mylist = <FILENAME>;
close FILENAME;

where would i put perl -lne 'print unless /^\s*$/' ?

thanks,
gammaman
 
Code:
[b]#!/usr/bin/perl[/b]

@mylist = ( 'one',
            '',       # ignore
            'three',
            '',       # ignore
            'five' );
            
foreach $value (@mylist) {
  print "$value\n" unless $value =~ m/^$/;
}


Kind Regards
Duncan
 
You can change Trojan's one to:
Code:
perl -ni.bak -e 'print unless /^\s*$/' filename
That's create a backup file called filename.bak too, just in case it all goes horribly wrong. Incidentally, the -l option is superflous here, as the end-of-line marker in the regexp will match before the endline anyway.
 
Code:
#!/usr/bin/perl

@mylist = ( 'one',
            '',       # ignore
            'three',
            '',       # ignore
            'five' );
            
foreach $value (@mylist) {
  print "$value\n" unless $value =~ m/^$/;
}

Its wierd, but I ran this on a file with 1000 lines and it worked great, but when i ran it against against a file with 9700 lines, it didn't remove the blanks, but printed the exact file again. is there a limitation in perl that i'm not aware of?

thanks,
gammman
 
I seriously doubt it - but i am afraid i am not wise to the issues of file size limitations. I have, however, opened files much larger than 9700 lines - which i consider to be a pretty small file. However, lets get rid of the entire file being put into the array - which could be the problem.

Try this:-

Code:
[b]#!/usr/bin/perl[/b]

open (INFILE, "< bigFile9700lines.txt");

while (<INFILE>) {
  print "$_\n" unless /^$/;
}

close INFILE;


Kind Regards
Duncan
 
Hmmmm

How about /^\s*$/ as a pattern instead; that will account for lines which blank except for whitespace characters.

Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
I totally agree Mike - that was pretty short-sighted of me!


Kind Regards
Duncan
 
thanks for your responses. with your comments, I melded everything together to create this, and as fas as I can see, it looks fine
Code:
open FILENAME, "file1";
while(<FILENAME>)
{
	next if /^\s*$/;  
	chomp;            
	push @myarray, $_;   
}
close FILENAME;

open FILENAME_TWO, ">results";
foreach $item (@myarray)
{
	print FILENAME_TWO "$item\n";
}
close FILENAME_TWO;
yet...for large files, it still refuses to work. i needed to put it in an array because it was the only way i knew how to print to a file. anyways thanks for the comments! :)
 
Did you try Duncan's suggestion with the while() loop instead of slurping the entire file into an array? That's the recommended method of processing files line-by-line.
 
yeah, the results appear to be the same with or without an array

Code:
open (INFILE, "<tmp1");
open RESULTS, ">results";

while (<INFILE>) {
        chomp; 
        print RESULTS "$_\n" unless /^\s*$/;
}

close INFILE;
close ERASE_BLANK;

to be more accurate, both sets of code remove some blank lines while leaving other blank lines alone. I havn't found a pattern to it, which is why i'm having such a hard time debugging. blanks caused by spaces, returns, or tabs seem to yield the same result.

thanks,
gammaman
 
The 'blank' lines that make it through - are they really blank? Or do they have weird non-display characters in them? AFAIK '\s' only matches space, \t, \n, \r, and \f (what is \f? form feed?) so if you have any other bytes less than ASCII(32) it may cause the effect you are seeing. Try
Code:
use strict;
use warnings;

my $nondisp = join("", (chr(0)..chr(31)));

while (<DATA>) {
        chomp;
        print "$_\n" unless /^[$nondisp]*$/o;
}
and see if it fixes it.
 
stevexff,

you're right, i think there's some wierd characters that i'm not seeing. When I open the processed file up in windows notepad, the blanks are gone. But when I open it up in an editor such as textpad or editpad, the blanks show up. I tried your code, but it didn't seem to have any effect. Now I just need to figure out what these characters are.

thanks,
gammaman
 
OK

My code only removes anything less than ASCII 32, if yours are above this they won't get removed.

They might be Unicode characters that aren't being handled correctly? What version of perl are you running - 5.8 has better Unicode support.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top