473,404 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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 4041
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

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

Similar topics

0
by: Shawn Mehaffie | last post by:
I have the following class that I've wirtten to take a Dataset and automatically export it to either XML, ASCII or Tab delimited file. The reason I wrote it they way I did was that I don't want to...
4
by: N. Graves | last post by:
Thanks for taking the time to read this note. I have a Access inventory collection tool that I have automated to build and Export to a CSV file. In the database I have several fields that are...
5
by: Reggie | last post by:
Hi and TIA! I have a query that I want to save as a fixed-width text file. I can get the export to work using an export specification with one exception. This is a government message and they need...
4
by: Jay | last post by:
This is a strange one that I can't seem to find a fix for. We have a Billing DB application (Access 2000 format) where we upload billing info in a comma delimited text file to our printer who...
0
by: Shawn Mehaffie | last post by:
I have the following class that I've wirtten to take a Dataset and automatically export it to either XML, ASCII or Tab delimited file. The reason I wrote it they way I did was that I don't want to...
1
by: forumaic | last post by:
Hello, I am trying to export data to excel from datagrid, and I am getting an error: "The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)." ...
5
by: The Pipe | last post by:
Hello I have a table that I need to export to a .asc file format. I have had several attempts at getting this to work but with no luck. Basically the file would need to have every record...
3
by: Wayne | last post by:
I'm trying to automate the export of a query to a text file using code. If I export the query manually I get the exact result that I want i.e. If I select the query then choose File/Export from...
3
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I have a question for you. I have a .csv file which has many lines of data. Each line has many data fields which are delimited by ",". Now I need to extract part of data from this...
4
by: grumpydadtl | last post by:
I am exporting a text file from Access that will be fed into a mainframe application (third party - not something I manage). The file contains a header record, body - which is the variable number of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.