1832960 Members
3033 Online
110048 Solutions
New Discussion

Help script parse file

 
SOLVED
Go to solution
Jairo Campana
Trusted Contributor

Help script parse file

Hi, I need parse file :

in my script input a field pattern: number "node":

example:
----------------------------
./switch
Input number node: 20:20:20:20
-----------------------------

the script found node :20:20:20:20 and display the name of hosts ,and lines:
Type Pid COS PortName NodeName SCR
FC4s: FCP
NodeSymb: [41] "QLA2460 FW:v4.00.23 DVR:v9.1.2.19 (wx64)"
Port Name: 20:20:20:20
Permanent Port Name: 20:20:20:20:
Device type: Physical Initiator
Aliases:


*********** cat log ********************

HOS1
Version 5.1.0d
HOST1 login: user
Password:
user> node 20:20:20:20
No device found
user> Trying 10.10.10.10
Connected to 10.10.10.11
Escape character is '^]'.


HOST2
Version 5.1.0d
HOST2 login: user
Password:
user> node 20:20:20:20
Local:
Type Pid COS PortName NodeName SCR
N 338a00; 3;20:20:20:20:78:58;20:20:20:20; 3
FC4s: FCP
NodeSymb: [41] "QLA2460 FW:v4.00.23 DVR:v9.1.2.19 (wx64)"
Port Name: 20:20:20:20
Permanent Port Name: 20:20:20:20:
Device type: Physical Initiator
Aliases:


... more lines...
HOSTS 3
...

**************************


legionx
20 REPLIES 20
Peter Nikitka
Honored Contributor

Re: Help script parse file

Hi,

first close your duplicate thread of the same topic.
The idea is to
- collect data into variables,
- check for your anchor data
- output data after a block delimiter
I assume 2 or more empty lines as a block delimiter
Without coding your request completely, this should lead you to one correct way:

awk -v mynode=20:20:20:20 '$2 == "login:" {host=$1;next}
$2 == "node" {if($3==mynode) node=1;else node=0}
/^Type Pid / {tp=$0;getline;tpdata=$0;next}
# more coding
!NF {empty++;next}
empty && NF {if (node && empty) printf("Host %s\nnode %s\n%s\n%s\n",host,mynode,tp,tpdata)
empty=node=0;tp=tpdata=""}' log

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Hein van den Heuvel
Honored Contributor

Re: Help script parse file

Jairo,

It seems to me you simplified that input and output beyond (my) recognition.
If the replies up to this point do not help you enough, then please consider a reply with TEXT attachment with more input, and EXACT output.

- There are a lot of 20:20:20:20 occurences in the input. Which one is the trigger?
- I suspect the line "N 338a00; 3;20:20:20:20:78:58;20:20:20:20; 3" is the trigger. First or second occurance?
- That line is NOT in the sample output. Intentional?
- The words HOSTx are NOT in the output. Intentional?
- Does the first line to display start with 'Type' and the last with 'Aliases'?
- All lines in between, or just some?

Anyway... here is a perl script which seems to do what you currently requested.
See how close it is to what you want!
Enjoy,
Hein.

#!/usr/bin/perl

use strict;
use warnings;

my $LOG = 'cat.log';
my $target = shift or die "Please provide a search target string";
my $found = 0;
my @lines;

open (LOG, "<$LOG") or die "Could not open log file : $LOG\n$!";

while () {
@lines = () if /^Type/;
if (/^N.*;$target;/) {
$found++;
} else {
push @lines, $_;
}
if ($found && /^Alias/) {
print @lines;
$found = 0;
}
}






Jairo Campana
Trusted Contributor

Re: Help script parse file

I try from promt shell , obtaing :
# mynode=20:20:20:20

# awk -v mynode=20:20:20:20 '$2 == "login:" {host=$1;next} $2 == "node" {if($3==20:20:20:20) node=1;else node=0} /^Type Pid / {tp=$0;getline;tpdata=$0;next} !NF {empty++;next} empty && NF {if (node && empty) printf("Host %s\nnode %s\n%s\n%s\n",host,20:20:20:20,tp,tpdata) empty=node=0;tp=tpdata=""}' log

Obtaing :
awk: syntax error near line 1
awk: bailing out near line 1
#
legionx
Jairo Campana
Trusted Contributor

Re: Help script parse file

Hei van den , your script works , only not obtaing the name host name.

the log its incomplete

send to original log output

address search 21:00:00:1b:32:06:78:58

the address can be in several name of hosts


legionx
Hein van den Heuvel
Honored Contributor

Re: Help script parse file

>> Hei van den , your script works , only not obtaing the name host name.
>> the log its incomplete

Which is exactly how you requested it according to the output example in the base topic. Correct?

Anyhow, it is easier to show it. Change this:

From:
if (/^N.*;$target;/) {
$found++;
} else {
push @lines, $_;
}
To:
$found++ if /^N.*;$target;/
push @lines, $_;

You may also want to change that RSE to /^N.*;$target/

By dropping the final semicolon, you can abbreviate teh node number match string.

Hein.




Jairo Campana
Trusted Contributor

Re: Help script parse file

>>Which is exactly how you requested it >>according to the output example in the >>base topic. Correct?

yes its correct , see original log attach in before post
legionx
Peter Nikitka
Honored Contributor

Re: Help script parse file

Hi,

I'm shure my awk works, when corretly formatted. When run not under HP-UX (Solris e.g.) use 'nawk' instead.

awk -v mynode=20:20:20:20 '$2 == "login:" {host=$1;next}
$2 == "node" {if($3==20:20:20:20) node=1;else node=0}
/^Type Pid / {tp=$0;getline;tpdata=$0;next}
!NF {empty++;next}
# the printf-statement is on a single line
empty && NF {if(node && empty) printf("Host %s\nnode %s\n%s\n%s\n",host,20:20:20:20,tp,tpdata)
empty=node=0;tp=tpdata=""}' log

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Jairo Campana
Trusted Contributor

Re: Help script parse file

the awk script , is only one line?
legionx
Dennis Handly
Acclaimed Contributor

Re: Help script parse file

>the awk script, is only one line?

It doesn't look like it. It is one long string with embedded newlines quoted by ''.
Peter Nikitka
Honored Contributor
Solution

Re: Help script parse file

Hi,

my comment just notified the fact that on the ITRC page the printf-line is seen spread over two lines. If you execute the command, you should put it on only one line.

line1:
awk -v mynode=20:20:20:20 '$2 == "login:" {host=$1;next}
line2:
$2 == "node" {if($3==20:20:20:20) node=1;else node=0}
line3:
/^Type Pid / {tp=$0;getline;tpdata=$0;next}
line4:
!NF {empty++;next}
line5:
empty && NF {if(node && empty) printf("Host %s\nnode %s\n%s\n%s\n",host,20:20:20:20,tp,tpdata)
line6:
empty=node=0;tp=tpdata=""}' log

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Jairo Campana
Trusted Contributor

Re: Help script parse file

obtaing this error:
run from solaris 8

# nawk -v mynode=20:20:20:20 '$2 == "login:" {host=$1;next} $2 == "node" {if($3==mynode) node=1;else node=0} /^Type Pid / {tp=$0;getline;tpdata=$0;next} !NF {empty++;next} empty && NF {if (node && empty) printf("Host %s\nnode %s\n%s\n%s\n",host,mynode,tp,tpdata) empty=node=0;tp=tpdata=""}' cat.log
nawk: syntax error at source line 1
context is
$2 == "login:" {host=$1;next} $2 == "node" {if($3==mynode) node=1;else node=0} /^Type Pid / {tp=$0;getline;tpdata=$0;next} !NF {empty++;next} empty && NF {if (node && empty) printf("Host %s\nnode %s\n%s\n%s\n",host,mynode,tp,tpdata) >>> empty <<< =node=0;tp=tpdata=""}
nawk: illegal statement at source line 1
#
legionx
Peter Nikitka
Honored Contributor

Re: Help script parse file

Hi,

it seems, you insist in putting all awk statements into one line (why?).
If you do not want to use the line layout of my previous post, the last error message you sent could be resolved in putting a semicolon before the 'empty=...' statement:

...,mynode,tp,tpdata); empty=node=...

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Jairo Campana
Trusted Contributor

Re: Help script parse file

good ,this works , but I try run the script with other log .

see attacht in post before.




legionx
Jairo Campana
Trusted Contributor

Re: Help script parse file

that change to execute it with log attach in before post.

legionx
Peter Nikitka
Honored Contributor

Re: Help script parse file

Hi,

I'm shure, you can do this by yourself.
You've got the idea for parsing such a file - now is the time for YOU to do the homeworks.

Some helpful comments:
- change 'node' to 'nodefind'
- lever the condition for 'Type Pid' to be located at the beginning of a line, so that there may be some spaces before:
/^ *Type Pid /
- add more pattern-action lines to this code to collect all required output.

Have fun!
mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Jairo Campana
Trusted Contributor

Re: Help script parse file

attach teh real log.
legionx
Jairo Campana
Trusted Contributor

Re: Help script parse file

attach the real log
legionx
Jairo Campana
Trusted Contributor

Re: Help script parse file

I apply sed in file , removes all empty lines from the file , see attach

I change node for nodefind
legionx
Jairo Campana
Trusted Contributor

Re: Help script parse file

I obtaing , without name the hosts and display all hosts

backup2# ./test1
Host login:
node 21:00:00:1b:32:06:78:58


Host login:
node 21:00:00:1b:32:06:78:58


Host login:
node 21:00:00:1b:32:06:78:58


Host login:
node 21:00:00:1b:32:06:78:58


Host login:
node 21:00:00:1b:32:06:78:58
Type Pid COS PortName NodeName SCR
N 338a00; 3;21:00:00:1b:32:06:78:58;20:00:00:1b:32:06:78:58; 3
Host login:
node 21:00:00:1b:32:06:78:58
Type Pid COS PortName NodeName
N 338a00; 3;21:00:00:1b:32:06:78:58;20:00:00:1b:32:06:78:58;
Host login:
node 21:00:00:1b:32:06:78:58


Host login:
node 21:00:00:1b:32:06:78:58


Host login:
node 21:00:00:1b:32:06:78:58


Host login:
node 21:00:00:1b:32:06:78:58


Host login:
node 21:00:00:1b:32:06:78:58
Type Pid COS PortName NodeName SCR
N 338a00; 3;21:00:00:1b:32:06:78:58;20:00:00:1b:32:06:78:58; 3
backup2#
legionx
Jairo Campana
Trusted Contributor

Re: Help script parse file

Thanks.
legionx