Steve Jennings
asked on
Parse lines in a large text file
My file looks like this:
P1ACC01
interface g1/1
interface g1/2
interface g1/3
switchport mode trunk
interface g1/4
interface g1/5
switchport mode trunk
P1RLM01
interface g1/1
interface g1/2
interface g1/3
switchport mode trunk
interface g1/4
switchport mode trunk
interface g1/5
switchport mode trunk
interface g1/6
and on and on
The lines beginning "P1" are hostnames and will always start with P1. If an "interface" line is followed by a "switchport mode trunk" line, then I need to capture that for each host. If an interface line is followed by another interface line (meaning that it isn't a 'trunk' line) then I don't want to capture it.
So, I would like the output for a script run against the data lines above to look like:
P1ACC01
interface g1/3
switchport mode trunk
interface g1/5
switchport mode trunk
P1RLM01
interface g1/3
switchport mode trunk
interface g1/4
switchport mode trunk
interface g1/5
switchport mode trunk
I am basically trying to produce a report of the interfaces that are configured as trunks on each host.
Here's what I have so far (feel free to laugh, it's ok) and it doesn't work.
open (FILE, "</work/projects/interface s.txt");
while ($line =<FILE>)
{
if ($line =~ m/^P1.*/)
{
$host=$line;
}
$nextline = <FILE>;
if ($nextline = /.interface./)
{
$line1=$nextline;
$nextline1 = <FILE>;
if ($nextline1 = /.trunk./)
{
printf "$host, $line, $line1\n";
}
}
}
In fact, this produces nothing.
Thanks for your help.
Steve
P1ACC01
interface g1/1
interface g1/2
interface g1/3
switchport mode trunk
interface g1/4
interface g1/5
switchport mode trunk
P1RLM01
interface g1/1
interface g1/2
interface g1/3
switchport mode trunk
interface g1/4
switchport mode trunk
interface g1/5
switchport mode trunk
interface g1/6
and on and on
The lines beginning "P1" are hostnames and will always start with P1. If an "interface" line is followed by a "switchport mode trunk" line, then I need to capture that for each host. If an interface line is followed by another interface line (meaning that it isn't a 'trunk' line) then I don't want to capture it.
So, I would like the output for a script run against the data lines above to look like:
P1ACC01
interface g1/3
switchport mode trunk
interface g1/5
switchport mode trunk
P1RLM01
interface g1/3
switchport mode trunk
interface g1/4
switchport mode trunk
interface g1/5
switchport mode trunk
I am basically trying to produce a report of the interfaces that are configured as trunks on each host.
Here's what I have so far (feel free to laugh, it's ok) and it doesn't work.
open (FILE, "</work/projects/interface
while ($line =<FILE>)
{
if ($line =~ m/^P1.*/)
{
$host=$line;
}
$nextline = <FILE>;
if ($nextline = /.interface./)
{
$line1=$nextline;
$nextline1 = <FILE>;
if ($nextline1 = /.trunk./)
{
printf "$host, $line, $line1\n";
}
}
}
In fact, this produces nothing.
Thanks for your help.
Steve
perl -pe'BEGIN{$/="trunk"}s#(in terface.*\ n)+(?!.*$/ )##' /work/projects/interfaces. txt
ASKER
Thanks to you both . . . .honestly . . . for responding so quickly on a Friday night. But I'm lost:
jeromee . . . your code produced:
Can't find string terminator "'" anywhere before EOF at -e line 1.
ozo . . . your code produced:
P1ACC01
interface g1/1
interface g1/2
interface g1/3
switchport mode trunk
interface g1/4
interface g1/5
switchport mode trunk
P1RLM01
interface g1/1
interface g1/2
interface g1/3
switchport mode trunk
interface g1/4
switchport mode trunk
interface g1/5
switchport mode trunk
interface g1/6
I downloaded Active Perl 5.12 . . . no luck.
Thanks again,
Steve
jeromee . . . your code produced:
Can't find string terminator "'" anywhere before EOF at -e line 1.
ozo . . . your code produced:
P1ACC01
interface g1/1
interface g1/2
interface g1/3
switchport mode trunk
interface g1/4
interface g1/5
switchport mode trunk
P1RLM01
interface g1/1
interface g1/2
interface g1/3
switchport mode trunk
interface g1/4
switchport mode trunk
interface g1/5
switchport mode trunk
interface g1/6
I downloaded Active Perl 5.12 . . . no luck.
Thanks again,
Steve
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks jeromee and ozo . . .
Glad to help!
ASKER
jeromee or ozo . . . could you please annotate your scirpts? I would be happy to open another question and give you points for the annotation.
Thanks
SteveJ
Thanks
SteveJ
Here you go:
perl -ne "print if/^P1/; print qq($p$_) if/switchport mode trunk/; $p=$_;" your_big_file
-ne : open the file(s) passed as arguments (in this case your_big_file) and apply the following code to every line
print if/^P1/; : print the line if it starts (note the ^) with the string P1
print qq($p$_) if/switchport mode trunk/; : if the line contains the string "switchport mode trunk", print the previous line ($p) and the current line ($_). Note the qq($stuff) notation which is equivalent to "$stuff). qq() was used here because the one-line code was already delimited by "".
$p=$_ : save the value of the current line to be used as previous line when processing the next line
your_big_file : file to be processed
perl -ne "print if/^P1/; print qq($p$_) if/switchport mode trunk/; $p=$_;" your_big_file
-ne : open the file(s) passed as arguments (in this case your_big_file) and apply the following code to every line
print if/^P1/; : print the line if it starts (note the ^) with the string P1
print qq($p$_) if/switchport mode trunk/; : if the line contains the string "switchport mode trunk", print the previous line ($p) and the current line ($_). Note the qq($stuff) notation which is equivalent to "$stuff). qq() was used here because the one-line code was already delimited by "".
$p=$_ : save the value of the current line to be used as previous line when processing the next line
your_big_file : file to be processed
ASKER
Thanks jeromee!! Turns out the big_file had a lot more garbage in it than I anticipated and I'm having to cobble a little more together to get the result that I wanted. The difficulty for me is reading a line, then reading another line . . . then reading a third line and handling the first line based on what's in the second or third line.
Thanks
Steve
Thanks
Steve
Create another question with the details and we'll take a look at it.
ASKER
Thanks jeromee.
perl -ne'print if/^P1/; print "$p$_" if/switchport mode trunk/; $p=$_' your_big_file