Julie Xu wrote:
>
> Greeting,
Hello,
> I am trying to create a simple perl script to read a text file (8 columns
> separated by space) and output the colume6,8.
>
> The file format is:
> Entry11 entry12 entry13 entry14 entry15 entry16 entry17 entry18
> Entry21 entry22 entry23 entry24 entry25 entry26 entry27 entry28
> ..................
perl -lane'print"@F[5,7]"' yourfile.txt
> The script is following:
> #!/usr/bin/perl -w
> use perltext;
> use strict;
> use Fcntl ':flock'; # contains LOCK_EX (2) and LOCK_UN (8) constants
>
> my $buffer;
> my $file;
>
> $file = $ARGV[0];
You can declare and define the variable in one statement.
my $file = $ARGV[0];
> open(INFILE, $file);
You should _ALWAYS_ verify that the file opened correctly!
open INFILE, $file or die "Cannot open $file: $!";
> # request an exclusive lock on the file.
> flock(INFILE, LOCK_EX);
You should verify that flock locked the file.
flock INFILE, LOCK_EX or die "Cannot flock $file: $!";
> # read in each line from the file
> while (<INFILE>)
> {
> #split out the fields in the line
> @entry = split / /;
/ / will split on a single space. It would be better to use split with
no arguments which will split on multiple whitespace.
my @entry = split;
> print "Entry: $entry[6] Entry: $entry[8]\n";
Arrays in perl start at 0 so the sixth element will be $entry[5] and the
eighth element will be $entry[7].
> }
> print "Entry6: $entry[6] Entry8: $entry[8]\n";
> # unlock the file.
> flock(INFILE, LOCK_UN);
> close(INFILE);
You don't need to unlock the file as closing the file will automatically
unlock it.
> the output I want is:
> Entry: xxxxxx Entry: xxxxxx
>
> Could anyone help me with this script since it can not get correct output?
perl -lane'print"Entry: $F[5] Entry: $F[7]"' yourfile.txt
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]