Sponsored Content
Top Forums Shell Programming and Scripting Perl - if conditions is meet, push the last field of $_ into an array Post 302266537 by popeye on Wednesday 10th of December 2008 12:39:19 PM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
lindex(n)						       Tcl Built-In Commands							 lindex(n)

__________________________________________________________________________________________________________________________________________________

NAME
lindex - Retrieve an element from a list SYNOPSIS
lindex list ?index...? _________________________________________________________________ DESCRIPTION
The lindex command accepts a parameter, list, which it treats as a Tcl list. It also accepts zero or more indices into the list. The indices may be presented either consecutively on the command line, or grouped in a Tcl list and presented as a single argument. If no indices are presented, the command takes the form: lindex list or lindex list {} In this case, the return value of lindex is simply the value of the list parameter. When presented with a single index, the lindex command treats list as a Tcl list and returns the index'th element from it (0 refers to the first element of the list). In extracting the element, lindex observes the same rules concerning braces and quotes and backslashes as the Tcl command interpreter; however, variable substitution and command substitution do not occur. If index is negative or greater than or equal to the number of elements in value, then an empty string is returned. The interpretation of each simple index value is the same as | for the command string index, supporting simple index arithmetic and indices relative to the end of the list. If additional index arguments are supplied, then each argument is used in turn to select an element from the previous indexing operation, allowing the script to select elements from sublists. The command, lindex $a 1 2 3 or lindex $a {1 2 3} is synonymous with lindex [lindex [lindex $a 1] 2] 3 EXAMPLES
lindex {a b c} -> a b c lindex {a b c} {} -> a b c lindex {a b c} 0 -> a lindex {a b c} 2 -> c lindex {a b c} end -> c lindex {a b c} end-1 -> b lindex {{a b c} {d e f} {g h i}} 2 1 -> h lindex {{a b c} {d e f} {g h i}} {2 1} -> h lindex {{{a b} {c d}} {{e f} {g h}}} 1 1 0 -> g lindex {{{a b} {c d}} {{e f} {g h}}} {1 1 0} -> g SEE ALSO
list(n), lappend(n), linsert(n), llength(n), lsearch(n), lset(n), lsort(n), lrange(n), lreplace(n), string(n) | KEYWORDS
element, index, list Tcl 8.4 lindex(n)
All times are GMT -4. The time now is 09:36 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy