Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2004
    Posts
    1

    Unanswered: search for string within a file

    I'm having problems trying to create a perl scrip to do the following:

    1. open a file.
    2. search for a known string (i.e. "Filename=".
    3. then take the string which comes after the "Filename=" string.
    4. pass this string to a variable so i can use it to display somewhere else.

    example:
    a file contains "Filename=abcdefg.txt someother text" and i want to find the "Filename=" then pass the "abcdefg.txt" to a variable without passing "someother text" to the variable.

  2. #2
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    Code:
    my $file = '/path/to/file.txt';
    my $var = '';
    open(R_FILE,"<$file") or die $!;
    while(my $line = <R_FILE>){
      if($line =~ /Filename=){
        ($var) = $line =~ /Filename=(\S+)/;
      }
    }
    close R_FILE;
    
    print "The filename is:\t",$var,"\n";
    board.perl-community.de - The German Perl-Community

  3. #3
    Join Date
    Jun 2004
    Location
    Nowhere Near You
    Posts
    89
    or perhaps ...
    Code:
    #!\users\bin\perl -w
    
    my($s_Source)="MyFile.Dat";
    open(IN,"<$s_Source") or die "$!";
    foreach (map {/Filename=(\S*)/? ($1) : ()} (<IN>)) {
      # $_ is now the filename, so process $_
      print $_,"\n";
       };
    close(IN);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •