jamen_98 has asked for the wisdom of the Perl Monks concerning the following question:

Hi!

I have a question, not even sure where to start coding on. I have a file that has a field in brackets that I want to export to another file. For some reason it doesn't pick up the file unless the brackets are removed.
Here's the script that works without brackets:

#$slotnumber=0; $dpdrivloc = "Trinity"; open(DPSLOTFILE, "< c:\\test.txt") or die "Can't open file: $!"; while ( <DPSLOTFILE> ) { if ( /$dpdrivloc:\s*(\d+)/ ) { print "Slot for $dpdrivloc: $1\n"; } } #system("c:\\ipbat.bat"); close DPSLOTFILE;

The file has brackets around "Trinity" and won't read data unless the brackets are removed.
The file works if the file looks like:

Label Location Time NKL0800 MSL6000 Trinity: 7 1800

The code doesn't work when file is like:
Label Location Time [NKL0800] [MSL6000 Trinity: 7] 1800

Unfortunately the file that needs to be read is like the second one. What I would like to do is take the fields inside [] and copy the data to a new file for reading. Any ideas (stripping out from [], I can handle the file I/O)???

Edited by BazB fixed formatting

Replies are listed 'Best First'.
Re: Remove field inside [ ] brackets
by delirium (Chaplain) on Mar 22, 2004 at 15:43 UTC
    Your string with brackets is being evaluated as a regular expression, not a string. Try using \Q and \E to escape the regular expression-ish characters, e.g.:

    if ( /\Q$dpdrivloc\E:\s*(\d+)/ )

      s/expression-ish characters/meta characters/;#)
Re: Remove field inside [ ] brackets
by neniro (Priest) on Mar 22, 2004 at 15:58 UTC
    I'cant find an error - the following works fine for me:
    #!/usr/bin/perl -w use strict; #$slotnumber=0; my $dpdrivloc = "Trinity"; open( DPSLOTFILE, "< c:\\perlen\\perlmonks\\test.txt") or die "Can't open file: $!"; while ( <DPSLOTFILE> ) { if ( /$dpdrivloc:\s*(\d+)/ ) { print "Slot for $dpdrivloc: $1\n"; } } #system("c:\\ipbat.bat"); close DPSLOTFILE;
    Don't forget to use strict; and -w.
      Okay, I figured it out...  The file is output from 
      another program, I created a batch file:
      
      
      rem Convert to text format type test.txt > testnew.txt
      From there, I made a call to that batch file:
      system("c:\convert.bat");
      I then ran the code against testnew.txt and it worked!!! Wahoo!!!!! THANK YOU SO MUCH!!!!!
        The file is output from another program...

        Based on your evidence, I would guess that the other program runs on some other (non-Microsoft) OS, or else has some other reason for producing text with just "LF" instead of "CRLF" line termination. If you get to understand the nature of your input data better, you can get Perl to deal with it directly -- set the "$/" (input record separator) variable to match whatever that other program is using at the end of each line.

        Perl is actually a useful tool for this sort of closer inspection of input data. Consider:

        #!/usr/bin/perl use strict; $/ = undef; # go into "slurp" mode $_ = <>; # read whole file at once # (this assumes the file name is given in @ARGV) my @chars = split //; # each array element holds on character my $nlf = grep /\x0a/, @chars; my $ncr = grep /\x0d/, @chars; printf( "Input contained %d characters, %d line-feeds, %d carriage-ret +urns\n", scalar @chars, $nlf, $ncr );
Re: Remove field inside [ ] brackets
by xorl (Deacon) on Mar 22, 2004 at 17:38 UTC
    Can you just remove all brackets and write the data to a new file?
    open(DPSLOTFILE, "< c:\\test.txt") or die "Can't open file: $!"; while ( <DPSLOTFILE> ) { my $buffer .= $_; } $buffer =~ s/\[|\]//g; close DPSLOTFILE; open(NEWFILE, ">+C:\\output.txt"); print NEWFILE $buffer; close(NEWFILE);
    Note: Code untested and may not even be the right way to do this.

    EDIT: Looks like someone found the solution while I was typing this.