How to export a text line to a new file?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sevla
    New Member
    • May 2009
    • 16

    How to export a text line to a new file?

    Hello,

    i need to make a search of a text line on a dci/txt file and after i found it, i´d need to export this line to another new file.


    could it be possible, copying a specific line of this dci/txt file and export it to a separated dci/txt file?



    Thanks
  • SonECrocket
    New Member
    • Dec 2008
    • 3

    #2
    Something to this effect might work for you

    Code:
    open(INPUTFILE, "<inputfilename") or die "Can't open input file";
    open(OUTFILE, ">outputfilename") or die "Can't open output file";
    @lines=<INPUTFILE>;
    @greplines=grep(/"Line to search for"/, @lines);
    if (@greplines)
    {
      foreach $match (@greplines)
      {
        print OUTFILE $match."\n";
      }
    }
    Last edited by eWish; May 28 '09, 01:10 AM. Reason: Please use the [code][/code] tags

    Comment

    • Sevla
      New Member
      • May 2009
      • 16

      #3
      its exactly what i wanted man,

      thanks a lot

      just a obs: the output file overwrites one over one each time a new searching is made, can store in different files each searching?

      here it goes my code:

      Code:
      #!C:/perl/bin/perl.exe
      #Handle Area - in - outfile
      
      open(INPUTFILE, "< c:/perl/discoverEdit[1].4796.11.30.5.8.2009.dci") or die "Can't open input file";
      open(OUTFILE, "> c:/perl/resultreport.dci") or die "Can't open output file";
      @read=<INPUTFILE>;
      @lines=<INPUTFILE>;
      chomp @read;	
      
      #searching area
      
             print "type something\n";
                     $choose=<STDIN>; chomp $choose;
                     my @greplines = grep(/$choose/,@read); 
             print "Sorry,Not Found !!" 
             unless(@greplines);  
             		foreach $match (@greplines) { 
       		print OUTFILE $match."\n";        
      
      }
      Thanks Once Again!!
      Last edited by eWish; May 28 '09, 01:10 AM. Reason: Please use the [code][/code] tags

      Comment

      • eWish
        Recognized Expert Contributor
        • Jul 2007
        • 973

        #4
        Sevla,

        I had added code tags for you several times of the past couple of days. You need to include these at the time you make your post.

        Here is an example of how to use the code tags.

        &#91;CODE]Perl code here&#91;/CODE]

        --Kevin

        Comment

        • nithinpes
          Recognized Expert Contributor
          • Dec 2007
          • 410

          #5
          If you do not want the results to be over-written, open the output file in append mode.
          Code:
          open(OUTFILE, ">>c:/perl/resultreport.dci") or die "Can't open output file";

          Comment

          • Sevla
            New Member
            • May 2009
            • 16

            #6
            thanks a lot, it worked perfectly now

            i just have one more thing to boring you with guys,

            i had to make this whole search, by word , outputing in a outfile, to get me familiar with perl and start on programming enviroment, and now to finally finish this little program at my job i need to make the search by the specific line , once it had numbered the file lines,

            if someone can give a little help here it would be nice.

            heres the code:

            Code:
            #File Open Area
            
            open FILE, "c:/perl/discoverEdit[1].4796.11.30.5.8.2009.dci" || die "failed to open notice.txt $!\n";
            @read=<FILE>; 
            close FILE;
            open(OUTFILE, "> c:/perl/report.dci") or die "Can't open output file";
            @lines= <FILE>;
            chomp @read;	
            
            #searching area
            
            
                   print "type the line\n";
                           $choose=<STDIN>; chomp $choose;
                           my @greplines = grep(/$lines/, @read); 
                           foreach $line (@greplines) { 
                           print OUTFILE $line."\n";
                     
            
            }
            }

            Thank You

            Comment

            • mledbetter
              New Member
              • May 2009
              • 2

              #7
              Just get the total number of elements in your @read array.

              Code:
              $lineCount = @read;
              
              # Or just get the index of the last element of the array.
              $lineCount = $#read;
              # Since these are zero based arrays, you have to add 1
              $lineCount++;
              
              #Select a specific line
              $lineToSelect = $read[<index>];

              Hope that helps. I haven't written perl in a long time. I just found this forum because I have a perl project and needed a refresher.

              Also, in your code you appear to have an error.
              You closed the filehandle <FILE> but then asign it to @lines.
              Did you mean @lines = <OUTFILE>; ?
              I'm not sure why you would need that line.

              -Matt

              Comment

              • Sevla
                New Member
                • May 2009
                • 16

                #8
                yes

                i mean with this code search for a line and then this line is outputed in a new file with the line i wrote


                and on searching area

                wich way i declare to make me search for a line on command and output it on a report.txt file ?

                Comment

                • Sevla
                  New Member
                  • May 2009
                  • 16

                  #9
                  i finally got this code

                  search by line and export it to a new file

                  it worked pretty nice

                  Code:
                  #!C:/perl/bin/perl.exe
                  #Handle Area - in - outfile
                  
                  open my $file, q{c:/perl/discoverEdit[1].4796.11.30.5.8.2009.dci} or die "Can't open input file";
                  open(OUTFILE, "> c:/perl/report.dci") or die "Can't open output file";
                  my @read = <$file>;
                  print "This file has: " .scalar(@read) . " lines \n";
                  print "Which line you want to export?\n";
                  chomp (my $match = <STDIN>);
                  if ($match < scalar(@read)) {   
                      print OUTFILE $read[$match];
                  
                  }
                  thanks for your kind patience!!

                  Comment

                  Working...