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.

(split)[3]

Here is "grep" part of my perl script so far (ommitting the various
iterations of split,substr and gw that don't work):

--------snip------------------
open INPUT, "539.t";
open OUTPUT, "plr";

It is confusing to have an input file handle called OUTPUT.

Did you perhaps mean to open plr for output? If not you really should
think of a better name.

Note the old bareword global filehandles used in Perl4 are better
avoided most of the time in Perl5. As is the old two parameter open in
which the filename an mode are merged into a single argument. Perl does
not throw an execption if open() fails it simply sets the special
variable $! and returns false. To convert this into an exception you
need to die().

open my $input, '<', "539.t" or die $!;
open my $output , '>' , "plr" or die $!;

while ( <INPUT> ) {
chomp;

@TAPENAME = grep /POSITIONING/, ($_);
print @TAPENAME;

}

Ok, there a significant number of misconceptions.

The perl grep function loops over list of strings you pass it.

You are calling grep _within_ a loop processing one line at a time. You
do not want two nested loops.

You should get rid of one or other and say something like...

while ( <$input> ) {
if (/POSITIONING/) {
print ((spilt)[3]);
}
}

....or...

my @lines = grep /POSITIONING/, <$input>;
# Do whatever with lines

.



Relevant Pages

  • Re: grep for Windows (was: Re: [OT] Creating a Music database)
    ... > Grep for Windows, if you can make sense of it... ... So I opened the folder itself, and lo and behold, there was a "bin" folder ... I use a lot of Gnu utilities - I copy the executables and DLLs to a common ...
    (rec.pets.cats.anecdotes)
  • Find a line in a text file then print a field
    ... My employer is moving our backup software and robots from ... I had written a crude ksh script that grep'ed a line ... to do the same thing on Windows and perl seems like the right way to ... open OUTPUT, "plr"; ...
    (perl.beginners)
  • Re: Flame Bait! Windows vs: The Unices
    ... >> consistancy of Windows (compared to UNIX) one of its strong points. ... But, in effect, Windows only runs on one platform,... ... Describing the *difference* between using grep in unix and using it ... just use an IDE and move on. ...
    (comp.programming)
  • Re: TechWriter version 8.40
    ... On 28 Jun 2005 Martin Wuerthner wrote: ... > files causing crashes or loops and I have added a lot of new features ... the editor to provide PDF in future! ... "You can't have Windows without pains." ...
    (comp.sys.acorn.apps)
  • Re: Data Mining for PIX Firewall Logs
    ... My response was not intended to imply that there was no other way to "grep" ... than with Cygwin. ... I am sure there is an open source grep for Windows. ... that tells me you are logging to a Windows machine. ...
    (Pen-Test)