how to substitute strings in a newly created file 
Author Message
 how to substitute strings in a newly created file

I'm writing a script to make files of directory listings.  However, I'd
like to strip the extensions off all files listed in the new file, but
can't seem to get the syntax correct:

#!/public/bin/perl -w


closedir FLTCOR;

open(RSP, ">rsp.list");

while (<>)
{
s/.*txt.*//g; # extension to be removed is ".txt"

Quote:
}

The script seems to run into infinity at this point, the file is
created, however.

Any ideas?

Thanks

..Tom

-----------------------------------------------------------------------------------------
Tom Kralidis                                  Geo-Spatial Technologist
Canada Centre for Remote Sensing              Tel: (613) 947-1828
588 Booth Street , Room 241                   Fax: (613) 947-1408
Ottawa , Ontario K1A 0Y7                     http://www.*-*-*.com/
-----------------------------------------------------------------------------------------



Sun, 20 Jan 2002 03:00:00 GMT  
 how to substitute strings in a newly created file


Quote:
>I'm writing a script to make files of directory listings.  However, I'd
>like to strip the extensions off all files listed in the new file, but
>can't seem to get the syntax correct:

>#!/public/bin/perl -w

Good use of the -w tag, but you should have 'use strict' too.

Quote:
>opendir FLTCOR, "fltcor" or die "can't open directory: $!";

>closedir FLTCOR;

>open(RSP, ">rsp.list");

You ought to check the return value of this too ('or die(...);').

Quote:

>while (<>)
>{
>s/.*txt.*//g; # extension to be removed is ".txt"

This is going to remove any line with "txt" in it, which I don't think you
want.

Quote:
>}

You've written the list to the file before you've modified it, and the
magic <> filehandle thingie is probably waiting for you to enter something
on STDIN. Try this instead:

#
#

closedir FLTCOR;

open (RSP, ">rsp.list") or die "Can't open rsp.list: $!";

  s/\.txt$//g;   #This should work better.
  print RSP;

Quote:
}

close (RSP) or warn "Can't close rsp.list: $!";
#
#

HTH! Good luck.

Matt
--
Matthew Zimmerman ------------   http://www.*-*-*.com/ .{*filter*}ia.edu/~mdz4c
Interdisciplinary Biophysics Program --------- University of {*filter*}ia
| "You got to be very careful if you don't know where you're going, |
| because you might not get there."                   -- Yogi Berra |



Sun, 20 Jan 2002 03:00:00 GMT  
 how to substitute strings in a newly created file


Quote:
> I'm writing a script to make files of directory listings.  However, I'd
> like to strip the extensions off all files listed in the new file, but
> can't seem to get the syntax correct:

> #!/public/bin/perl -w

use strict;

Quote:
> opendir FLTCOR, "fltcor" or die "can't open directory: $!";

OK.  So why don't you do that on the file-open below also?

Quote:

> closedir FLTCOR;

> open(RSP, ">rsp.list");


Don't you want one or more newlines in there?  You will have spaces,
though.

Quote:
> while (<>)
> {
> s/.*txt.*//g; # extension to be removed is ".txt"
> }

> The script seems to run into infinity at this point, the file is
> created, however.

You are asking for input at that point, from any files named in the
argument list or from STDIN.  No wonder it waits a long time.

Even if the loop were meaningful, the regex is not.  You say you want to
strip off all extensions, and you have to do that before writing the
output file.  So:


That leaves alone filenames without extensions, such as '.login' or
'foobar.'.

Yes, gurus, the regex could be written with positive look-behind, but so
what?  And I am assuming filenames without newlines.  Let's not get
carried away.  I have never seen anyone knowingly name a file with a
newline.  What a neat way to{*filter*}up an `ls`, no?

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.*-*-*.com/



Sun, 20 Jan 2002 03:00:00 GMT  
 how to substitute strings in a newly created file

Quote:
>I'm writing a script to make files of directory listings.  However, I'd
>like to strip the extensions off all files listed in the new file, but
>can't seem to get the syntax correct:

>#!/public/bin/perl -w

One point for using -w.  One point deduction for not using strict.

Quote:
>opendir FLTCOR, "fltcor" or die "can't open directory: $!";

One point for checking opendir for success.


can process them just a easily one by one and reduce memory usage.

Quote:
>closedir FLTCOR;

>open(RSP, ">rsp.list");

One point deduction for not checking if open worked.

Here you write the complete contents of your directory (sans . and ..)
to rsp.list, blank separated with nary a linefeed.  Not to mention that
no extension stripping has been done at this point.

Quote:
>while (<>)

Look up what the empty filehandle does.

Quote:
>{
>s/.*txt.*//g; # extension to be removed is ".txt"

If this regular expression ever gets to see a line, it will reduce it
to emptiness if it contains 'txt' anywhere.  I don't think that's what
you want.

Quote:
>}

>The script seems to run into infinity at this point, the file is
>created, however.

No.  Assuming you called the script with no arguments, it's waiting
for input from the keyboard.

Quote:
>Any ideas?

The following will put the filenames minus extension on a line each
in rsp.list.  You'll get an empty line if a file is extension only.

#!/usr/bin/perl -w
use strict;

my $dir = 'fltcor';
my $list = 'rsp.list';

opendir FLTCOR, $dir or die "Can't open directory $dir: $!\n";
open RSP, ">$list" or die "Can't write $list: $!\n";
while ( $_ = readdir( FLTCOR) ) {
  next if m/^\.\.?$/;  # don't process . and ..
  s/\.[^.]+$//;        # crush extension
  print RSP "$_\n";

Quote:
}

__END__

Anno



Mon, 21 Jan 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Opening a newly created file.

2. Creating new file and substituting data

3. Substituting many sparsely distrbuted strings in many files

4. Substituting $variable strings in a file

5. substituting strings in many files

6. Substituting Strings in Many Files

7. Substituting strings (with newlines) in many files

8. split and substitute, substitute, substitute

9. Substituting many strings in one pass over the input

10. substituting strings in source trees?

11. Pattern Match - substitute a string after the match

12. Repeat string matching/substituting ?

 

 
Powered by phpBB® Forum Software