Help with foreach loop and extracting a certain string.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with foreach loop and extracting a certain string.
# 1  
Old 07-10-2008
Help with foreach loop and extracting a certain string.

Hi there,

I would like to have some assistance with 2 problems I have.
Well, let's call it 2 challenges.
The language to use is Perl.

First is a foreach loop (or any other suggestion).
I want to have a list of IP addresses and exit when an extracted IP address is not in that list.
It is important to have the IP's listed line by line.
Here a first try, but it gives an error:

foreach my $i (
"10.216.74.137"
"10.216.74.144"
"10.216.74.148"
) {
return 0 unless ( $agent_address eq $i );
}

Second "challenge" is to retrieve the last part of a line:

This is the line (part of a bigger message):

enterprises.alcatel.nmu.genos.alarmHandoff.alarmHandoffObjects.alarmTable.alarmEntry.probableCause.3 200904 = Replaceable Unit Missing

Currently I have this:

my $probablecause;
if ($msg_line[15] =~ m/probableCause.*\.(.*)$/) {
$probablecause=$1;
}

But this returns everything after the search string probableCause.
The number behind probableCause always differs.
The output should be "Replaceable Unit Missing".

Many thanks in advance,

Erik
# 2  
Old 07-10-2008
Just a couple of suggestions that may help you.

I would put the IP address into an array. The loop through and look for the agent_address:

i.e.

@ips = qw(10.216.74.137 10.216.74.144 10.216.74.148);
$agent_address = "10.216.74.144";
foreach(@ips) {
print "$_\n" if(/$agent_address/);
}

And for the second one, maybe using a search\replace regex to grab the end of the string:

i.e.


$probablecause = "enterprises.alcatel.nmu.genos.alarmHandoff.alarmHandoffObjects.alarmTable.alarmEntry.probableCause. 3 200904 = Replaceable Unit Missing";
$probablecause =~ s/.*probableCause.*\s=\s(.*)/$1/;
print "$probablecause\n";

Hope this helps.
# 3  
Old 07-10-2008
If they wanted to check the entire IP address for an exact match they would use "eq" instead of a regexp:

Code:
@ips = qw(10.216.74.137 10.216.74.144 10.216.74.148);
$agent_address = "10.216.74.144";
foreach(@ips) {
   print "$_\n" if ($_ eq $agent_address);
}

Your way will find false positives especially because the dot character in the IP addresses will be interpreted as a wild card character in the regexp instead of a dot. You would want to use the \Q option to avoid that interpretation of the dot in an m// regexp.

Personally I do not understand this requirement:

Code:
I want to have a list of IP addresses and exit when an extracted IP address is not in that list.

"Exit" what? The only way to make sure the IP address is not in the list is to check the entire list, unless the list is in some type of order and you know once you get passed a certain part of the list the IP address can no longer be in the list. Like the letter "c" can not be past the letter "d" in an alpha sorted list of a-z.

Assuming the dot is always present after probableCause in the string:

Code:
$msg_line = 'enterprises.alcatel.nmu.genos.alarmHandoff.alarmHandoffObjects.alarmTable.alarmEntry.probableCause.3 200904 = Replaceable Unit Missing';
my $probablecause;
if ($msg_line =~ m/probableCause\.(?:[\d ]*)=([\S\s]+)$/) {
   $probablecause = $1 || 'no match';
}
print $probablecause;

# 4  
Old 07-11-2008
Hi,

Thanks to you both.

The first problem is solved with this:

my $continue = 0;
@ips = qw(
10.216.66.137
10.216.74.137
10.216.74.144
10.216.74.148
);
foreach(@ips) {
if ( $agent_address eq $_ ) { $continue = 1 };
}
return 0 unless $continue;


The second problem is solved with this:

my $probablecause;
if ($msg_line[15] =~ m/probableCause\.(?:[\d ]*)=([\S\s]+)$/) {
$probablecause=$1;
}

This because the last suggestion is a bit shorter and can be reused more easily.

Works great. Does the trick for me.

Erik
# 5  
Old 07-11-2008
Okay, perhaps one more thing.
One of the SNMP trap lines is as follows:

enterprises.alcatel.nmu.genos.alarmHandoff.alarmHandoffObjects.alarmTable.alarmEntry.currentAlarmId. 3200904 = Counter32: 3200904

This code:

my $currentalarmid;
if ($msg_line[11] =~ m/currentAlarmId\.(?:[\d ]*)=([\S\s]+)$/) {
$currentalarmid=$1;
}

returns this value:

Counter32: 3200904

Which is of course correct.
I would like to have the value 3200904 only.

Of course the value 3200904 in the line changes all the time.
The rest of the line is fixed.

Would I completely understand the magic in "\.(?:[\d ]*)=([\S\s]+)$/" I may solve it myself, but since I don't .............

Any suggestions ?

Regards,

Erik
# 6  
Old 07-11-2008
Code:
if ($msg_line[11] =~ m/currentAlarmId\.(?:[\d ]*)= Counter32: (\d+)$/)

# 7  
Old 07-11-2008
How simple some things can be :-)

Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Foreach loop with two variables

I need to put together a script that will take the contents of two different files (database name and database owner) and put them in two variables within a line: foreach x (`cat /local/hd3/dba/tools/build_db_scripts/dbs`) foreach z (`cat /local/hd3/dba/tools/build_db_scripts/dbas`)... (6 Replies)
Discussion started by: deneuve01
6 Replies

2. UNIX for Dummies Questions & Answers

foreach loop in csh

Hi everyone I'm new to unix and encountered a small problem i couldnt find out a reason why it doesn't work..please help.. in my csh script when i tried to use the foreach loop like this: foreach x ( ls ) echo $x end when i tried to run it, it printed out 'ls' to the std out instead of... (3 Replies)
Discussion started by: ymc1g11
3 Replies

3. Shell Programming and Scripting

foreach loop problem

Dear all, I wrote a script to download files and move files in directories according to their name. Now here is the problem: Both p101 and p360 data download successfully, but when I move them according to the year and month, only p101 data can be placed at the right location, p360,... (1 Reply)
Discussion started by: handsonzhao
1 Replies

4. UNIX for Dummies Questions & Answers

Using the Foreach loop, Needing help

I am trying to make a script for my Counter-Strike: Source servers. What i am wanting it to do is for it to restart each server, the only way i can think of doing this in through for each. Years what i have at the moment. server_start() { START=`ps x | grep SCREEN | grep $SRV | cut -d '?' -f... (5 Replies)
Discussion started by: grahamn95
5 Replies

5. Shell Programming and Scripting

Using sed with a foreach loop

So I am back again beating my head against the wall with a shell script and getting a headache! I want to change each year in a file (1980, 1981, 1982, 1983, etc.) to the same year followed by a tab. The input is "blah blah (1980) blah blah". I want to get "blah blah (1980 ) blah blah".... (2 Replies)
Discussion started by: Peggy White
2 Replies

6. UNIX for Advanced & Expert Users

Problem with foreach loop

Hi All, Is there any problem with the below 'foreach' loop? foreach risk_factor ($(cat "$rf_list")) where "rf_list=$SCRIPT/Utility/rflist.txt " I'm wondering, it is throwing below error message: syntax error at line 34: `(' unexpected Any idea/suggestions ? Thanks in advance /... (7 Replies)
Discussion started by: ganapati
7 Replies

7. Shell Programming and Scripting

foreach loop

Hi everyone Does anyone know what is wrong with this script. i keep getting errors foreach filename (`cat testing1`) set string=$filename set depth=`echo "$string" echo $depth end the error is the following testing: line 1: syntax error near unexpected token `(' testing: line 1:... (3 Replies)
Discussion started by: ROOZ
3 Replies

8. Shell Programming and Scripting

foreach loop + 2 variables

In a foreach loop, is it possible for the loop to go through 2 arguments instead of one i.e. instead of foreach i (do stuff for i), we have foreach i j(do stuff for i; do stuff for j) I am working under BASH and TCSH shell environments cheers (3 Replies)
Discussion started by: JamesGoh
3 Replies

9. Shell Programming and Scripting

foreach loop

Hi Guys, I have a loop which uses a wildcard i.e. foreach f (*) but when I execute the tcsh file in unix then it gives me an error ->>>>>>>foreach: words not parenthesized<<<<<<<<<<- Any help. (1 Reply)
Discussion started by: abch624
1 Replies

10. Shell Programming and Scripting

Foreach loop

What am I doing wrong with this foreach loop? foreach var ($argv) @sum = $sum + $var (4 Replies)
Discussion started by: haze21
4 Replies
Login or Register to Ask a Question