Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word after match 1

Status
Not open for further replies.

cam24r

ISP
May 17, 2016
5
US
Hi,

I am new to programming and new to Perl and I could definitely use some help. I have a .csv file from which I am reading lines, I used the following code:
foreach my $cell (@cells_array) {
$attr1_value = shift(@attr1s);


while ($attr1_value ne "") {
$_ = $attr1_value;
/inet filter\s+/;
@after=split(/\s+/,$');

/term\s+/;
@after2=split(/\s+/,$');

print "ACL Name is ";
print $after[0],"\n";

print "term is ";
print $after2[0],"\n";

$attr1_value = shift(@attr1s);
}
}
The problem I have is that if there is no match in @after2, then it will print the value from after. i tried changing the name of the $_ variable but when I do so I get an error complaining about the ' after the $ in line @after=split(/\s+/,$');

Any help is greatly appreciated.



trhead I sued for reference:
thread219-444636
 
I don't see in your code what it has to do with reading of a CSV file.
If you only want to parse fields from the CSV line, simply split it into an array.

The best would be, when you show us your input file and say what should be the output.

 
@Mikrom, thanks for your prompt response.

I want to get the word "inet filter" and after "term" if they exist.
This is my input:

Rule | Command
1 | set firewall inet filter ACL1 interface-specific
2 | set firewall inet filter ACL2 term Remark2 from prefix-list COMCAST-INFRASTRUCTURE
3 | set firewall inet filter ACL3 term Remark3 from tcp-established
4 | set firewall inet filter ACL4 from udp

This is my output:
ACL1 ,
ACL2 , Remark2
ACL3 , Remark3
ACL4 ,
 
To capture this fields you can use regular expressions and back references,
for example like here:

cam24r.pl
Code:
[COLOR=#804040][b]use strict[/b][/color];
[COLOR=#804040][b]use warnings[/b][/color];

[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$DBG_INFO[/color] = [COLOR=#ff00ff]0[/color];

[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$line[/color];
[COLOR=#804040][b]while[/b][/color]([COLOR=#008080]$line[/color] = [COLOR=#008080]<DATA>[/color] ) {
  [COLOR=#804040][b]chomp[/b][/color]([COLOR=#008080]$line[/color]);
  [COLOR=#008080]&process_line[/color]();
}

[COLOR=#804040][b]sub [/b][/color][COLOR=#008080]process_line [/color]{
  [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#008080]$line[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color] [COLOR=#804040][b]if[/b][/color] [COLOR=#008080]$DBG_INFO[/color];

  [COLOR=#804040][b]my[/b][/color] [COLOR=#008080]@line_list[/color] = ();
  [COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$line_out[/color] = [COLOR=#ff00ff]""[/color];
  [COLOR=#0000ff]# pattern 1[/color]
  [COLOR=#804040][b]if[/b][/color] ([COLOR=#008080]$line[/color] =~ [COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]inet filter[/color][COLOR=#6a5acd]\s[/color][COLOR=#6a5acd]+([/color][COLOR=#6a5acd]\w[/color][COLOR=#6a5acd]+)[/color][COLOR=#804040][b]/[/b][/color]) {
    [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#ff00ff]found1 = '[/color][COLOR=#008080]$1[/color][COLOR=#ff00ff]'[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color] [COLOR=#804040][b]if[/b][/color] [COLOR=#008080]$DBG_INFO[/color];
    [COLOR=#804040][b]push[/b][/color] [COLOR=#008080]@line_list[/color], [COLOR=#008080]$1[/color];
  }
  [COLOR=#0000ff]# pattern 2[/color]
  [COLOR=#804040][b]if[/b][/color] ([COLOR=#008080]$line[/color] =~ [COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]term[/color][COLOR=#6a5acd]\s[/color][COLOR=#6a5acd]+([/color][COLOR=#6a5acd]\w[/color][COLOR=#6a5acd]+)[/color][COLOR=#804040][b]/[/b][/color]) {
    [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#ff00ff]found2 = '[/color][COLOR=#008080]$1[/color][COLOR=#ff00ff]'[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color] [COLOR=#804040][b]if[/b][/color] [COLOR=#008080]$DBG_INFO[/color];
    [COLOR=#804040][b]push[/b][/color] [COLOR=#008080]@line_list[/color], [COLOR=#008080]$1[/color];
  }
  [COLOR=#0000ff]# print result[/color]
  [COLOR=#008080]$line_out[/color] = [COLOR=#804040][b]join[/b][/color]([COLOR=#ff00ff]"[/color][COLOR=#ff00ff],[/color][COLOR=#ff00ff]"[/color], [COLOR=#008080]@line_list[/color]);
  [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#008080]$line_out[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color]
}

[COLOR=#0000ff]# your log data[/color]
[COLOR=#0000ff]__DATA__[/color]
[COLOR=#0000ff]Rule | Command[/color]
[COLOR=#0000ff] 1 | set firewall inet filter ACL1 interface-specific[/color]
[COLOR=#0000ff] 2 | set firewall inet filter ACL2 term Remark2 from prefix-list COMCAST[/color]
[COLOR=#0000ff] 3 | set firewall inet filter ACL3 term Remark3 from tcp-established[/color]
[COLOR=#0000ff] 4 | set firewall inet filter ACL4 from udp[/color]

Output:
Code:
C:\_mikrom\Work>perl cam24r.pl

ACL1
ACL2,Remark2
ACL3,Remark3
ACL4

If you set $DBG_INFO = 1, you will see better how it works.
 
@mikrom Thank you so much, this worked as expected. I appreciate your help.
 
@cam24r
I am glad that I could help you. I wish you much fun with Perl.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top