Jessica <[EMAIL PROTECTED]> writes:
> I am such a knuckle head... I read the lists. I stumble through books and still I
>cant figure out how to do something that I would think should be relativly simple.
>
> I am attempting to extract a two words or fields from every line of a tab delimited
>file. Sometimes the words that I need are in fields 1 & 2 and other time they are in
>2 & 3. The lines look something like this:
>
> TypeOfApp ApplicationName RunStatus
> ---------------------------------------------------------------------
>
> application run_me_now complete <- Need 2 & 3
> application2 ran_me_yesterday complete/errors <- Need 2 & 3
> need_to_run_tomorrow failed <- Need 1 & 2
>
> I am in need of just extracting the ApplicationName and RunStatus from the lines and
>placing them in variables so that I can do things with them.
>
> I think I'm smart enought to know how to get the file opened and get the lines into
>a while loop like so:
>
> open (FILE, ./datafile);
>
>
> while (<FILE>) {
> do something really smart here
> }
>
> But its just the "do something really smart here" thats got me baffled... (Grin)
Well, we need to figure out what *exactly* the criteria is for getting
what you want out of the datafile. How can we tell the diffence
between the kind of line where you need fields 1 & 2, and the kind
where we need fields 2 & 3?
>From your diagram, it looks like the input file has three
tab-delimeted 'columns' - type, name, and status. However, on the
third line, it looks like the 'type' column is missing. I can't tell
for certain if there's supposed to be a tab there or not - the
following example assumes that there is not. If the line which seems
to be missing 'type' really has a tab in front of it, we would need a
different test (like changing the if in the example below to 'if
($line[0] =~ /\S/)' ). Also, if the 'tabs' are really several spaces
instead of actual tab characters, the regexp would need to be changed.
So, let's assume that 'Two tab-seperated words or three' is a definate
criteria we can use to tell the lines apart. Here's one way:
'
#!/usr/bin/perl -w
use strict;
open (FILE, './datafile');
while (<FILE>) {
chomp; #get rid of a newline at the end of each line
my @line = split /\t/; #split on tab characters
if (scalar @line == 3) {
print "Three fields found! '@line'\n"; #or whatever you need to do
}
elsif (scalar @line == 2) {
print "Two fields found! '@line'\n"; #or whatever...
}
else {
print "Unusual line! ",scalar @line," columns found. '@line'\n";
}
}
close FILE;
'
--
Robin Norwood
Red Hat, Inc.
"The Sage does nothing, yet nothing remains undone."
-Lao Tzu, Te Tao Ching
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]