Perl - if conditions is meet, push the last field of $_ into an array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl - if conditions is meet, push the last field of $_ into an array
# 1  
Old 12-10-2008
Perl - if conditions is meet, push the last field of $_ into an array

I am using a seed file shown below to separate cisco devices by ios/os type. I want to bunch all the devices based on ios/os version. Once I find a match, I only want to push the ip address into the appropriate array.

Example of seedfile

8 host1 (C3500XL-C3H2S-M) 11.0(5)WC17 10.1.44.21
9 host2 (C3500XL-C3H2S-M) 12.0(5)WC17 10.1.44.22
10 host3 (C3500XL-C3H2S-M) 12.0(5)WC17 10.1.44.23
11 host4 (cat4000-I9K91S-M) 12.2(25)EWA2 10.1.1.20
12 host5 (cat4000-I9K91S-M) 12.2(25)EWA2 10.1.1.21
13 host6 (cat4000-I9K91S-M) 12.2(25)EWA2 10.1.1.22
14 host7 (cat4000-I9K91S-M) 12.2(25)EWA2 10.1.1.23
15 host8 (cat4000-I9K91S-M) 12.2(25)EWA2 10.1.1.24
16 host9 (cat4000-I9K91S-M) 12.2(25)EWA2 10.1.1.25
17 host10 (cat4000-I9K91S-M) 12.2(25)EWA2 10.1.1.26
18 host11 (cat4000-I9K91S-M) 11.2(25)EWA6 10.1.1.27
19 host12 (cat4000-I9K91S-M) 12.2(25)EWA6 10.1.1.28
20 host13 (cat4000-I9K91S-M) 12.2(25)EWA2 10.1.1.29
21 host14 (C3550-I5Q3L2-M) 12.1(22)EA4 10.1.44.5
22 host15 WS-C6506 7.6(8) 10.1.48.10

open (INFILE, "seedfile") or die;
while (<INFILE>) {
chomp($_);
if ($_ =~ /\s\b[5-9]\./) {push(@dos, $_)};
if ($_ =~ /\s\b11\./) {push(@d110, $_)};
if ($_ =~ /\s\b12\.0/) {push(@d120, $_)};
if ($_ =~ /\s\b12\.1/) {push(@d121, $_)};
}
#test to make sure I know whats in "thar"
for ($lindex = 0; $lindex <= $#dos; $lindex++) {
print $dos[$lindex] . "\n";
}
close INFILE;

if $_ meets the requirements, I only want to push the ip address into the "d" arrays. $_[4] <- This obviously doesnt work, but illustrates what it is I want to parse.
# 2  
Old 12-10-2008
This works ... but it just seems that there is a better way ..
if ($_ =~ /\s\b[5-9]\./) {
@ip = split(/ /,$_);
push(@dos, $ip[4])};
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print lines that meet conditions and have value in another file

I am trying to use awk to print lines that satisfy either of the two conditions below: condition 1: $2 equals CNV and the split of $3, the value in red, is greater than or equal to 4. ---- this is a or so I think condition 2: $2 equals CNV and the split of $3, the value in red --- this is a... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Print header and lines that meet both conditions in awk

In the awk below I am trying to print only the header lines starting with # or ## and the lines that $7 is PASS and AF= is less than 5%. The awk does execute but returns an empty file and I am not sure what I am doing wrong. Thank you. file ... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. Programming

Javascript issue: array.push

I have a code snippet here that is supposed to vary values of certain parameter values: <script type="text/javascript"> // dynamic array of which its ultimate size is unknown // an array of arrays, each consisting of one variation var variations = ; // parameters of how to create a... (0 Replies)
Discussion started by: figaro
0 Replies

4. Shell Programming and Scripting

Perl - use search keywords from array and search a file and print 3rd field when matched

Hi , I have been trying to write a perl script to do this job. But i am not able to achieve the desired result. Below is my code. my $current_value=12345; my @users=("bob","ben","tom","harry"); open DBLIST,"<","/var/tmp/DBinfo"; my @input = <DBLIST>; foreach (@users) { my... (11 Replies)
Discussion started by: chidori
11 Replies

5. Shell Programming and Scripting

perl sum 2nd field in an array

Hi Everyone, ($total+=$_) for @record; assume @record=(1,2,3), so the result is 6. if @record=("1 3","2 3","3 3"), would like to sum up the 2nd field of this array, the result is 9. i tried " ($total+=$) for @record ", cannot, please advice. Thanks ---------- Post updated at 03:45... (1 Reply)
Discussion started by: jimmy_y
1 Replies

6. Shell Programming and Scripting

perl, put one array into many array when field is equal to sth

Hi Everyone, #!/usr/bin/perl use strict; use warnings; my @test=("a;b;qqq;c;d","a;b;ggg;c;d","a;b;qqq;c;d"); would like to split the @test array into two array: @test1=(("a;b;qqq;c;d","a;b;qqq;c;d"); and @test2=("a;b;ggg;c;d"); means search for 3rd filed. Thanks find the... (0 Replies)
Discussion started by: jimmy_y
0 Replies

7. Shell Programming and Scripting

perl sort array by field

Hi Everyone, Any simple code can simplify the code below, please advice. Thanks # cat 2.pl #!/usr/bin/perl use warnings; use strict; my @aaaaa = <DATA>; my @uids; foreach (@aaaaa) { my @ccccc = split (",", $_); push @uids, $ccccc;... (3 Replies)
Discussion started by: jimmy_y
3 Replies

8. Shell Programming and Scripting

Push records to array during implicit loop and write to file

NEWBIE ALERT! Hi, I'm 1 month into learning Perl and done reading "Minimal Perl" by Tim Maher (which I enjoyed enoumously). I'm not a programmer by profession but want to use Perl to automate various tasks at my job. I have a problem (obviously) and are looking for your much appreciated help.... (0 Replies)
Discussion started by: jospan
0 Replies

9. Shell Programming and Scripting

PERL, push to hash of array problem

$key = "a"; $value = "hello"; %myhash = {} ; push @{ myHash{$key} }, $hello; print $myHash{$key}."\n"; this script prints "hello" but has following error message. Reference found where even-sized list expected at ./test line 5. can any one help me to fix this problem?? (3 Replies)
Discussion started by: bonosungho
3 Replies

10. Shell Programming and Scripting

shift and push question in perl

hi, another perl question, I don't understand the below while (<FILE>) { push @last5, $_; #add to the end shift @last5 if @last5 > 5 ; #take from the beginning } can someone please explain to me how does shift @last5 if @last5 > 5 is taking last 5 lines from... (5 Replies)
Discussion started by: hankooknara
5 Replies
Login or Register to Ask a Question