Re: Find a line in a text file then print a field
- From: "kens" <kenslaterpa@xxxxxxxxxxx>
- Date: 14 Nov 2006 16:39:25 -0800
prichard@xxxxxxx wrote:
Background: My employer is moving our backup software and robots from
*nix to Windows. I had written a crude ksh script that grep'ed a line
from a text file and then awk'ed a field from that line. The field is
the name of a backup tape. Now I need to use something other than ksh
to do the same thing on Windows and perl seems like the right way to
go.
The text file:
Try 1
PROCESS 1159229039 3864 bptm
PROCESS 1159229039 1088 bpbrm
CONNECT 1159229039
CONNECTED 1159229039
MOUNTING 1159229044 FFU458
MOUNTED 1159229083 FFU458
POSITIONING 1159229089 FFU458 9
POSITIONED 1159229204 FFU458
BEGIN_WRITING 1159229204
END_WRITING 1159229324
Started 1159229028
KbPerSec 2437
Kilobytes 288690
Files 536
ActivePid 1724
RqstPid 2476
MainPid 2372
Status 0
DestStorageUnit ilmmtms3fp1_robot2
DestMediaServer ilmmtms3fp1
NumTapesToEject 1088
Ended 1159229333
I need to "grep" the line with the word "POSITIONING" in it and "awk"
out the 4th field, in this case FFU458. I used perl's grep to get the
right line but after much futzing with functions like split, gw and
others I can't get the 4th field.
Not certain I understand the reference to the 4th field. Looks
like the third field to me.
Here is "grep" part of my perl script so far (ommitting the various
iterations of split,substr and gw that don't work):
--------snip------------------
Always start your script with the following two lines:
use strict;
use warnings;
The 'use strict' will force you to declare your variables, but it will
save you time when you misspell a variable name.
open INPUT, "539.t";
open OUTPUT, "plr";
while ( <INPUT> ) {
chomp;
@TAPENAME = grep /POSITIONING/, ($_);
I think you are a little confused here. Usually grep is used to find
the matching lines in an array. Here you have forced a single line into
an array context. It would have made more sense to use grep if you had
'slurped' all the input into an array
my @lines = <INPUT>;
and then gotten all the matching lines:
grep /POSITIONING/,@lines;
However, it is probably just as easy to do the searching yourself using
a regular expression or split - see examples below.
print @TAPENAME;
}
close INPUT;
close OUTPUT;
--------snip------------------
I apologize ahead of time for idiocy and mis-understanding. I thank in
advance for assistance.
Any ideas?
There are many ways to do this. Below are a couple of examples:
#-----------------------------------------------------------------------------------------
# Using split
#----------------------------------------------------------------------------------------
use strict;
use warnings;
use English;
# append "\n" on each print
$OUTPUT_RECORD_SEPARATOR = "\n";
while ( <DATA> )
{
chomp;
my @fields = split(' ');
#------------------------------------------------------------------------------
# Make sure we returned 3 fields from split, so
# we do not reference an array element that does
# not exist.
# Then determine if the first field is the one we are
# looking for.
#------------------------------------------------------------------------------
if ($#fields >= 2 && $fields[0] eq 'POSITIONING')
{
print $fields[2];
}
}
__END__
#---------------------------------------------------------------------------------------------------------------
# using regular expression (for documentation issue the following
commands:
# perldoc perlretut
# perldoc perlrequick
# perldoc perlre
# perldoc perlreref
#---------------------------------------------------------------------------------------------------------------
while ( <DATA> )
{
chomp;
#-----------------------------------------------------------------------------------------------------
# note that the first line is equivalent to
# if (my ($TapeName) = $_ =~ //POSITIONING # Find POSITIONING
# The match returns a list of the items captured by parenthesis
#-----------------------------------------------------------------------------------------------------
if (my ($TapeName) = /POSITIONING # Find POSITIONING
\s+ # One or more whitespace
\w+ # One or more non-whitespace
\s+ # One or more whitespace
(\w+) # One or more non-whitespace
# value captured by parens
/x)
{
print $TapeName;
}
}
__DATA__
PROCESS 1159229039 3864 bptm
PROCESS 1159229039 1088 bpbrm
CONNECT 1159229039
CONNECTED 1159229039
MOUNTING 1159229044 FFU458
MOUNTED 1159229083 FFU458
POSITIONING 1159229089 FFU458 9
POSITIONED 1159229204 FFU458
BEGIN_WRITING 1159229204
END_WRITING 1159229324
Started 1159229028
KbPerSec 2437
Kilobytes 288690
Files 536
ActivePid 1724
RqstPid 2476
MainPid 2372
Status 0
DestStorageUnit ilmmtms3fp1_robot2
DestMediaServer ilmmtms3fp1
NumTapesToEject 1088
Ended 1159229333
.
- References:
- Find a line in a text file then print a field
- From: prichard@xxxxxxx
- Find a line in a text file then print a field
- Prev by Date: Re: Find a line in a text file then print a field
- Next by Date: Re: Perl - Bit Manipulation
- Previous by thread: Re: Find a line in a text file then print a field
- Next by thread: Re: SPUG: Minimal Perl talk at UW on Thursday
- Index(es):
Relevant Pages
|