Home > Archive > PERL Beginners > August 2005 > Read a single line in a file.
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Read a single line in a file.
|
|
| Umesh T G 2005-07-29, 5:01 pm |
| Hello List,
I have a file with multiple lines. I want to read only the first line.=20
and a particular word in that line.=20
For eg:=20
File.txt
apple,grape,orange,banana
some other lines
continued.
So I want only the word grape here. How to get it?
| |
| Arjun Mallik 2005-07-29, 5:01 pm |
|
----Original Message-----
From: Umesh T G [mailto:umesh.perl@gmail.com]=0D
Sent: Friday, July 29, 2005 3:47 PM
To: beginners@perl.org
Subject: Read a single line in a file.
Hello List,
I have a file with multiple lines. I want to read only the first line.=0D
and a particular word in that line.=0D
For eg:=0D
File.txt
apple,grape,orange,banana
some other lines
continued.
So I want only the word grape here. How to get it?
--=0D
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Hi ,
Follow the below procedure , may solve your problem. But be more prsice
with your problems.
----
Open( READ,"<File.txt"); --> open the file in read mode
$line=3D<READ>;
$line=3D~ /^[a-z]*,([a-z]*),.*/; ---> captures the second word of the
line.
Print "desired word is $1 \n";
---
Is this what u want ?
--
Arjun
Confidentiality Notice=0D
The information contained in this electronic message and any attachments to=
this message are intended
for the exclusive use of the addressee(s) and may contain confidential or=
privileged information. If
you are not the intended recipient, please notify the sender at Wipro or=
Mailadmin@wipro.com immediately
and destroy all copies of this message and any attachments.
| |
|
|
> ----Original Message-----
> From: Umesh T G [mailto:umesh.perl@gmail.com]
>
> Sent: Friday, July 29, 2005 3:47 PM
> To: beginners@perl.org
> Subject: Read a single line in a file.
>
>
> Hello List,
>
> I have a file with multiple lines. I want to read only the first line.
>
> and a particular word in that line.
>
>
> For eg:
>
> File.txt
>
> apple,grape,orange,banana
> some other lines
> continued.
>
> So I want only the word grape here. How to get it?
>
From the cookbook, recipe 8.8 "Reading a Particular Line in a File"
I use it and it works good. With this you can select any single line number,
not
just the first. You should be able to figure out how to extract a particular
word from the line
----
# usage: build_index(*DATA_HANDLE, *INDEX_HANDLE)
sub build_index {
my $data_file = shift;
my $index_file = shift;
my $offset = 0;
while (<$data_file> ) {
print $index_file pack("N", $offset);
$offset = tell($data_file);
}
}
# usage: line_with_index(*DATA_HANDLE, *INDEX_HANDLE, $LINE_NUMBER)
# returns line or undef if LINE_NUMBER was out of range
sub line_with_index {
my $data_file = shift;
my $index_file = shift;
my $line_number = shift;
my $size; # size of an index entry
my $i_offset; # offset into the index of the entry
my $entry; # index entry
my $d_offset; # offset into the data file
$size = length(pack("N", 0));
$i_offset = $size * ($line_number-1);
s ($index_file, $i_offset, 0) or return;
read($index_file, $entry, $size);
$d_offset = unpack("N", $entry);
s ($data_file, $d_offset, 0);
return scalar(<$data_file> );
}
# usage:
open(FILE, "< $file") or die "Can't open $file for reading: $!\n";
open(INDEX, "+>$file.idx")
or die "Can't open $file.idx for read/write: $!\n";
build_index(*FILE, *INDEX);
$line = line_with_index(*FILE, *INDEX, $s ing);
| |
| Octavian Rasnita 2005-07-30, 8:59 am |
| From: "Jim" <jkipp5@comcast.net>
>
>
Hi,
Use the script below:
open(FILE, $file_name);
my ($first, $second, $third, $fourth) = split /,/, <FILE>;
close FILE;
print $third;
Teddy
| |
| Tom Allison 2005-08-02, 4:59 pm |
|
> Hi,
>
> Use the script below:
>
> open(FILE, $file_name);
> my ($first, $second, $third, $fourth) = split /,/, <FILE>;
> close FILE;
>
> print $third;
>
> Teddy
>
>
my ($word) = (split /,/, <FILE> )[2];
|
|
|
|
|