×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Word after match

Word after match

Word after match

(OP)
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: How to read the next word after a keyword found

RE: Word after match

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.

RE: Word after match

(OP)
@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 ,

RE: Word after match

To capture this fields you can use regular expressions and back references,
for example like here:

cam24r.pl

CODE

use strict;
use warnings;

my $DBG_INFO = 0;

my $line;
while($line = <DATA> ) {
  chomp($line);
  &process_line();
}

sub process_line {
  print "$line\n" if $DBG_INFO;

  my @line_list = ();
  my $line_out = "";
  # pattern 1
  if ($line =~ /inet filter\s+(\w+)/) {
    print "found1 = '$1'\n" if $DBG_INFO;
    push @line_list, $1;
  }
  # pattern 2
  if ($line =~ /term\s+(\w+)/) {
    print "found2 = '$1'\n" if $DBG_INFO;
    push @line_list, $1;
  }
  # print result
  $line_out = join(",", @line_list);
  print "$line_out\n"
}

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

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.

RE: Word after match

(OP)
@mikrom Thank you so much, this worked as expected. I appreciate your help.

RE: Word after match

@cam24r
I am glad that I could help you. I wish you much fun with Perl.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close