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
8 4041
Something to this effect might work for you - 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";
-
}
-
}
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: - #!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!!
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
If you do not want the results to be over-written, open the output file in append mode. -
open(OUTFILE, ">>c:/perl/resultreport.dci") or die "Can't open output file";
-
-
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: -
-
#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
Just get the total number of elements in your @read array. -
$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
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 ?
i finally got this code
search by line and export it to a new file
it worked pretty nice -
-
#!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!!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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. <% ... %>)."
...
|
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...
|
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...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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 projectplanning, coding, testing,...
|
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...
| |