By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,933 Members | 1,686 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,933 IT Pros & Developers. It's quick & easy.

How to export a text line to a new file?

P: 16
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
May 27 '09 #1
Share this Question
Share on Google+
8 Replies


P: 3
Something to this effect might work for you

Expand|Select|Wrap|Line Numbers
  1. open(INPUTFILE, "<inputfilename") or die "Can't open input file";
  2. open(OUTFILE, ">outputfilename") or die "Can't open output file";
  3. @lines=<INPUTFILE>;
  4. @greplines=grep(/"Line to search for"/, @lines);
  5. if (@greplines)
  6. {
  7.   foreach $match (@greplines)
  8.   {
  9.     print OUTFILE $match."\n";
  10.   }
  11. }
May 27 '09 #2

P: 16
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:

Expand|Select|Wrap|Line Numbers
  1. #!C:/perl/bin/perl.exe
  2. #Handle Area - in - outfile
  3.  
  4. open(INPUTFILE, "< c:/perl/discoverEdit[1].4796.11.30.5.8.2009.dci") or die "Can't open input file";
  5. open(OUTFILE, "> c:/perl/resultreport.dci") or die "Can't open output file";
  6. @read=<INPUTFILE>;
  7. @lines=<INPUTFILE>;
  8. chomp @read;    
  9.  
  10. #searching area
  11.  
  12.        print "type something\n";
  13.                $choose=<STDIN>; chomp $choose;
  14.                my @greplines = grep(/$choose/,@read); 
  15.        print "Sorry,Not Found !!" 
  16.        unless(@greplines);  
  17.                foreach $match (@greplines) { 
  18.          print OUTFILE $match."\n";        
  19.  
  20. }
Thanks Once Again!!
May 27 '09 #3

eWish
Expert 100+
P: 971
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.

[CODE]Perl code here[/CODE]

--Kevin
May 28 '09 #4

nithinpes
Expert 100+
P: 410
If you do not want the results to be over-written, open the output file in append mode.
Expand|Select|Wrap|Line Numbers
  1. open(OUTFILE, ">>c:/perl/resultreport.dci") or die "Can't open output file"; 
  2.  
  3.  
May 28 '09 #5

P: 16
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #File Open Area
  3.  
  4. open FILE, "c:/perl/discoverEdit[1].4796.11.30.5.8.2009.dci" || die "failed to open notice.txt $!\n";
  5. @read=<FILE>; 
  6. close FILE;
  7. open(OUTFILE, "> c:/perl/report.dci") or die "Can't open output file";
  8. @lines= <FILE>;
  9. chomp @read;    
  10.  
  11. #searching area
  12.  
  13.  
  14.        print "type the line\n";
  15.                $choose=<STDIN>; chomp $choose;
  16.                my @greplines = grep(/$lines/, @read); 
  17.                foreach $line (@greplines) { 
  18.                print OUTFILE $line."\n";
  19.  
  20.  
  21. }
  22. }
  23.  
  24.  

Thank You
May 28 '09 #6

P: 2
Just get the total number of elements in your @read array.

Expand|Select|Wrap|Line Numbers
  1. $lineCount = @read;
  2.  
  3. # Or just get the index of the last element of the array.
  4. $lineCount = $#read;
  5. # Since these are zero based arrays, you have to add 1
  6. $lineCount++;
  7.  
  8. #Select a specific line
  9. $lineToSelect = $read[<index>];
  10.  

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
May 28 '09 #7

P: 16
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 ?
May 28 '09 #8

P: 16
i finally got this code

search by line and export it to a new file

it worked pretty nice

Expand|Select|Wrap|Line Numbers
  1.  
  2. #!C:/perl/bin/perl.exe
  3. #Handle Area - in - outfile
  4.  
  5. open my $file, q{c:/perl/discoverEdit[1].4796.11.30.5.8.2009.dci} or die "Can't open input file";
  6. open(OUTFILE, "> c:/perl/report.dci") or die "Can't open output file";
  7. my @read = <$file>;
  8. print "This file has: " .scalar(@read) . " lines \n";
  9. print "Which line you want to export?\n";
  10. chomp (my $match = <STDIN>);
  11. if ($match < scalar(@read)) {   
  12.     print OUTFILE $read[$match];
  13.  
  14. }
  15.  
  16.  
thanks for your kind patience!!
May 28 '09 #9

Post your reply

Sign in to post your reply or Sign up for a free account.