Reading each line of a file in perl script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading each line of a file in perl script
# 1  
Old 05-29-2008
Reading each line of a file in perl script

HI
I need to read each line (test.txt) and store it in a array (@test)
How to do it in perl.

Suppose i have a file test.txt. I have to read each line of the test.txt file and store it in a array @test. How to do it in perl.

Regards
Harikrishna
# 2  
Old 05-29-2008
Check if this helps...


Code:
TEST = "test.txt";
open(TEST) or die("Could not open log file.");
foreach $line (<TEST>) {
    chomp($line);              # remove the newline from $line.
    # do line-by-line processing.
}

# 3  
Old 05-29-2008
Code:
open (F, "test.txt") || die "Could not open test.txt: $!\n";
@test = <F>;
close F;

Perhaps you should read one of the many excellent Perl tutorials on the net rather than rely entirely on trial and error, though.
# 4  
Old 05-29-2008
Quote:
Originally Posted by era
Code:
open (F, "test.txt") || die "Could not open test.txt: $!\n";
@test = <F>;
close F;

Perhaps you should read one of the many excellent Perl tutorials on the net rather than rely entirely on trial and error, though.
Thanks alot era.... Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Error while reading variable from a file in perl script

I have a file abc.ini and declared many variables in that file, one of the variable(DBname) value I am trying to read in my perl script but getting error. File abc.ini content # database name DBname =UATBOX my $ex_stat; my $cmd_output; $ex_stat = "\Qawk '/^DBname/{print... (2 Replies)
Discussion started by: Devesh5683
2 Replies

2. Shell Programming and Scripting

Need help with perl script with a while loop reading file

Good morning, I appreciate any assistance that I can get from the monks out there. I am able to get this to work for me so that I can do a hostname lookup if I only specify one hostname in the script. What I want to do is have a file with hostnames and do lookups for each name in the file. Here is... (1 Reply)
Discussion started by: brianjb
1 Replies

3. Shell Programming and Scripting

Reading the file line by line in Perl

Hello Everyone, I have written a perl script that will load the entire data file into an array and then I would check the value of the specific column and then if interested I will write to a good file else I will write it to a bad file. But here, the problem is that if the data file is a... (15 Replies)
Discussion started by: filter
15 Replies

4. Shell Programming and Scripting

Perl Script for reading table format data from file.

Hi, i need a perl script which reads the file, content is given below. and output in new file. TARGET DRIVE IO1 IO2 IO3 IO4 IO5 ------------ --------- --------- --------- --------- --------- 0a.1.8 266 236 ... (3 Replies)
Discussion started by: asak
3 Replies

5. Shell Programming and Scripting

Help need in perl script reading from file

Need perl script, data file will be csv format. I have text file contains 2 colums. Filename Foldernumber aaaa 13455 bbbb 23465 cccc 26689 I have two location 1. files present and 2. folders present. I need to search for file and folder if folder... (3 Replies)
Discussion started by: hnkumar
3 Replies

6. Shell Programming and Scripting

SOLVED: reading config file in a perl script

Hi! I have a need to do this in Perl. script.pl -config file The script would be doing a wget/LWP on a URL which is defined in the config file. So when I run the script it should return either one of these conditions - 1) OK with exit status 0. Should also print "wget URL" 2)... (6 Replies)
Discussion started by: jacki
6 Replies

7. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

8. Shell Programming and Scripting

Help with perl script while reading a file

Hi Everyone, I am very new to perl, but came across a situation wherein I have to read a c++ header file and write the datatype, its identifier and also the length to an excel file. There can be other header files, in the directory but I should browse through the file which has only "_mef:" string... (9 Replies)
Discussion started by: ramakanth_burra
9 Replies

9. UNIX for Dummies Questions & Answers

perl - file reading - last line not displayed

Hi, Here is something that am trying with perl #! /opt/third-party/bin/perl open(fh, "s") || die "unable to open the file <small>"; @ch = (); $i = 0; while( $content = <fh> ) { if( $i <= 5 ) { push(@ch, $content); $i++; } else { $i = 1; foreach(@ch) { (8 Replies)
Discussion started by: matrixmadhan
8 Replies
Login or Register to Ask a Question