Perl: taking text from a .txt file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: taking text from a .txt file
# 1  
Old 06-19-2003
Java Perl: taking text from a .txt file

How would i go about the following,

I open a text document with loads of text, about 150 lines

e.g. "Best Time: 02:55.88"

How would i get perl just to take the time and put into a variable but also be able to take any time like that from the file??


i've done this so far to open the document


opendir (FILE, "> c:\\My Documents\\times.txt" or die "Cant open: $!";

readdir FILE @readfile;
closedir FILE;
# 2  
Old 06-19-2003
opendir/readdir is the wrong set of things to call for what you described. You want to "open" the file (I usually call it F if it's the only input file), then
Code:
while (<F>) {
    while (/\G(timepattern)/g) {
	print FILE $1, "\n"
    }
}

If you are supplying the file name on the command line, change "while (<F>)" to "while (<>)".

The inner while loop searches for all the substrings that match the regexp timepattern on an individual line. If you know there is at most one match for timepattern on a line, the inner "while" becomes "if (/(timepattern)/)". Constructing timepattern is a valuable enough skill that you should expend the effort to learn it on your own; see the perlre manpage. Or, as the textbooks say, it is left as an exercise for the reader.
# 3  
Old 06-19-2003
thanks very much!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parse through a txt file PERL scripting

Below is a perl code I am trying. #!/usr/bin/perl #use strict; use warnings qw/ all FATAL /; use constant ENV_FILE => '/apps/env_data.txt'; $uenv = $ARGV; my $input = $uenv; open my $fh, '<', ENV_FILE or die sprintf qq{Unable to open "%s" for input: $!}, ENV_FILE; ... (2 Replies)
Discussion started by: Tuxidow
2 Replies

2. Shell Programming and Scripting

Script extract text from txt file with grep

All, I require a script that grabs some text from the gitHub API and will grep (or other function) for a string a characters that starts with (") quotes followed by two letters, may contain a pipe |, and ending with ) . What i have so far is below but it's not returning anything. ... (4 Replies)
Discussion started by: ChocoTaco
4 Replies

3. Windows & DOS: Issues & Discussions

2 Questions: replace text in txt file, add text to end of txt file

so... Lets assume I have a text file. The text file contains multiple "#" symbols. I want to replace all thos "#"s with a STRING using DOS/Batch I want to add a certain TEXT to the end of each line. How can I do this WITHOUT aid of sed, grep or anything linux related ? (1 Reply)
Discussion started by: pasc
1 Replies

4. Shell Programming and Scripting

PERL or SHELL Scrript to search in Directories by taking line by line from a text file

Unix box server version *********** >uname -r B.11.00 >echo $SHELL /usr/bin/ksh --> in this server, I have the path like /IMbuild/dev/im0serv1 ---> in that directory I have the folders startup(.jsp files nearly 100 jsp's ) and scripts(contains .js files nearly 100 files) ... (9 Replies)
Discussion started by: pasam
9 Replies

5. Shell Programming and Scripting

PERL:How to convert numeric values txt file to PACKED DECIMAL File?

Is there any way to convert numeric values txt file to PACKED DECIMAL File using PERL. Regards, Alok (1 Reply)
Discussion started by: aloktiwary
1 Replies

6. Shell Programming and Scripting

taking output in csv file from perl

Hi, I am new to perl I need to connect from linux server to oracle database and i need to query the database and take result into csv file. i try to do but i am getting this error: #!/usr/bin/perl use DBI; BEGIN { $ENV{ORACLE_HOME} = '/home/oracle/product/8.1.7'; ... (1 Reply)
Discussion started by: prakash.gr
1 Replies

7. Shell Programming and Scripting

Help needed in extracting text present between two headers in .txt file

Hi All, Please help me out in fllowing problem. I have text file which contains the data in following format. Contents of file.txt are setregid02 Test that setregid() fails and sets the proper errno values when a non-root user attemps to change the real or effective... (2 Replies)
Discussion started by: varshit
2 Replies

8. Shell Programming and Scripting

Script for removing text from a txt file

Hello, So I wanted to write a very simple script to remove some information from a text file and save it as something else. For example I have a text file (let's call it txt) with three rows of numbers: 0 0 1 9 8 7 5 0 6 7 9 0 0 7 9 8 1 1 6 4 0 6 0 0 9 8 4 6 0 9 2 8 1 And I want to... (2 Replies)
Discussion started by: hertingm
2 Replies

9. Shell Programming and Scripting

Help need in modifying the text of .txt file

Hi All, I've written a shell script in which i defined two varibles for example: str=1.0.0.15 timeStamp=2008.03.08 now using this varibles i need to modify a text file. The text content looks like this ************************ * packageNumber : 1.0.0.14 * * date :... (2 Replies)
Discussion started by: vinna
2 Replies

10. Shell Programming and Scripting

Perl how to move pointer to previous line in a txt file?

I have a text file that has blocks of text. Each block starts with ### and ends with End_###. I wrote a perl script to search a string from line 2 (ignore any line starts with ###) of each block if matched, need to print that whole block. According to the input file in below, it will print... (5 Replies)
Discussion started by: tqlam
5 Replies
Login or Register to Ask a Question