Re: Find a line in a text file then print a field




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

.



Relevant Pages

  • Re: Using regular expressions
    ... To be able to do a grep on a file via a perl script do you have to read the whole file in before performing the search and replace? ... If this is the case how else could I do a grep to do a search and replace without using all the resources? ... >Could you give an example of using grep on a array to do a replace? ... >>printing the values to the output file. ...
    (perl.beginners)
  • Re: pack an array
    ... of this script to pack the array @array. ... Use the 'grep' function: ...
    (perl.beginners)
  • Re: using arrays in my bash script, having problems
    ... Ok, I realized that it would only take one array at a time, so I simply ... main script. ... Now I thought this would install the constants in my ... When my script gets to this grep line, ...
    (comp.unix.shell)
  • Re: Logon script - function array and select case not working
    ... this all works well, except, the function i am using for the rules in the control script causes alot of querrys to AD. as there are alot of groups. ... objTSout.writeline retrv ... So if you think that this will assign an array value to the variable, how do you think the case select statement is going go compare this array value with the literal string values such as "group name here"? ... However, by not assigning ANY value to checkgrp in the function, you are guaranteeing that, should the function ever exit, it will return no information. ...
    (microsoft.public.scripting.vbscript)
  • Re: Perl: Subroutines and use of the "GD::Graph::bars;" Module
    ... I have been looking at the script once again, I could not really get your suggestion to work, but I have been doing some new changes thought. ... @array = sort @array; ... sub verbose { ... Global symbol "$title" requires explicit package name at ./bars.pl line 53. ...
    (perl.beginners)