469,336 Members | 5,385 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

How to export a text line to a new file?

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
8 3740
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
Sevla
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
971 Expert 512MB
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
410 Expert 256MB
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
Sevla
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
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
Sevla
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
Sevla
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.

Similar topics

reply views Thread by Shawn Mehaffie | last post: by
5 posts views Thread by Reggie | last post: by
4 posts views Thread by Jay | last post: by
reply views Thread by Shawn Mehaffie | last post: by
1 post views Thread by forumaic | last post: by
3 posts views Thread by =?Utf-8?B?YzY3NjIyOA==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.