Visit Our UNIX and Linux User Community

REGEX problem

Thread Tools Search this Thread
Top Forums Programming REGEX problem
# 1  
Old 06-15-2010
REGEX problem

Hi there,
How can we use regex in perl to store the Route Distinguisher (the bold field) and also the underlined and bold lines in the below file?


These highlighted pattern is redundant through the whole input file. Basically, we just need to extract these fields at least to store them for later use using RegEx in perl and each Route Disnguisher has to go in a hash as the key with its corresponding underlined network patterns as values? The header should be skipped.

Here's our input file:
  Network            Next Hop         From             AS Path
 Route Distinguisher:  577:99     Local            ?
 Route Distinguisher:  577:10168     Local            ?        65500 ?
 Route Distinguisher:  700:700       65500 ?       65500 ?       65500 ?       65500 ?       Local            ?       65500 ?       65500 ?     Local            ?       65500 ?       65500 ?       65500 ?       65500 ?       65500 ?       65500 ?       65500 ?       65500 ?       65500 ?       65500 ?       65500 ?       65500 ?       65500 ?       65500 ?       65500 ?

Last edited by vgersh99; 06-15-2010 at 05:56 PM.. Reason: code tags, please!
# 2  
Old 06-15-2010
What I'm doing here is checking to see if a line has 'Route Distinguisher' in it, and if so then storing the key string in a variable ($buf). If not then I'm storing the values into a hash of arrays of arrays.

#! /usr/bin/perl -w

open(my $inpFile, "<", "inp.txt") || die $?;

my $buf;
my %HoAoA;


        if (m/Network\s+Next Hop\s+From\s+AS Path/) {
	if(m/.*Route Distinguisher.*/)
		my ($junk, $rd) = split(/:\W+/);
		$buf = $rd;
		my @arr = split(/\s+/);
		push @{ $HoAoA{$buf} }, [@arr];

close $inpFile;

I banged this out quickly so there's no validation, error checking, or warranty but it should get you started.

Here's an unwrap which outputs the Route Distinguisher followed by a colon then comma separated values for each row of input:

foreach $key (keys %HoAoA)
	foreach $rowArr ($HoAoA{$key})
		foreach $val (@$rowArr)
			print $key . ":" . join(',', @$val) . "\n";

Last edited by DeepakS; 06-17-2010 at 02:09 PM.. Reason: Added more code
# 3  
Old 06-16-2010
Here's a different way to address the same problem:

use strict;
use warnings;
use Data::Dumper;

open(FILE, "./data.txt" ) || die "Can't open file:$!\n";

my %results;
my $current_route;
my $i = 0;
while(<FILE>) {
        if ($_ =~ m/Network\s+Next Hop\s+From\s+AS Path/) {
        if ($_ =~ m/Route Distinguisher:\s+([0-9]+:[0-9]+)$/) {
                $i = 0;
        if ($_ =~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/) {
                my @line = split(/\s+/, $_);
                $results{$current_route}[$i]{'Network'} = $line[0];
                $results{$current_route}[$i]{'Next Hop'} = $line[1];
                $results{$current_route}[$i]{'From'} = $line[2];
                $results{$current_route}[$i]{'AS Path'} = $line[3];

print Dumper(\%results);

I skip the header, and then create a separate hash key for each Route Distinguisher, and create an array of the lines for each, which are hashes keyed by the column name.
# 4  
Old 06-17-2010
Nice, though that does fail in the case where there is a leading space on a record.

Also, I realized I forgot to skip the header (Smilie) - hope you don't mind my lifting from your code.
# 5  
Old 06-17-2010
Originally Posted by DeepakS
Nice, though that does fail in the case where there is a leading space on a record.

Also, I realized I forgot to skip the header (Smilie) - hope you don't mind my lifting from your code.
You're right. I munged the data slightly from the original post, and got rid of leading spaces, but that's easy enough to handle:

if ($_ =~ m/^\s*\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/) {

I don't mind you lifting from it at all. Happy to help.

Previous Thread | Next Thread
Test Your Knowledge in Computers #383
Difficulty: Medium
Solaris 2.5.1 was registered as UNIX 95 compliant on the PReP PowerPC platform.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl Regex problem

Script logs into switches on my list but nothing seems to happen. Following error: tr nope, doesn't (yet) match (?-xism:-]+ ?(?:\(config*\))? ? ?$) du SEEN: Here is code in question: @version_info = $session_obj->cmd('term length 0'); $session_obj->cmd('show int | i... (5 Replies)
Discussion started by: mrlayance
5 Replies

2. Programming

Regex problem

Hi, I am looking for regex to extract following words from text: The word which comes after "Replaced" means Replaced disk Replaced floppy Replaced memory Please suggest the regex for it. Thanks! (4 Replies)
Discussion started by: gunjanamit
4 Replies

3. Shell Programming and Scripting

awk regex problem

hi everyone suppose my input file is ABC-12345 ABCD-12345 BCD-123456 i want to search the specific pattern which looks like - in a file so i used this command cat $file | awk ' { if ($0 ~ /-/) { print } }' so it gives me the result as ABCD-12345 BCD-12345 BCD-12345 ... (31 Replies)
Discussion started by: aishsimplesweet
31 Replies

4. UNIX for Advanced & Expert Users

Sed regex problem

Hi, I tried to extract the time from `date` with sed. (I know it works with `date +%H:%M:%S` as well) I got three solutions of which just one worked. I thought "+" should repeat the previous expression 1 or more times and {n} should repeat the previous expression n times. $ date Thu... (9 Replies)
Discussion started by: thiuda
9 Replies

5. Shell Programming and Scripting

Need some help with a regex if loop problem

Need some help with a regex if loop problem. File1: 2323 3232 4230 3230 4340 4343 233 32320 I want to print "Zero" if the number ends with a zero, but print "number" if it does not! #!/bin/bash /usr/bin/nawk '{ if ($1 ==/+0\b/){ print "Zero"} else{ print "number"} (5 Replies)
Discussion started by: linuxkid
5 Replies

6. UNIX for Dummies Questions & Answers

regex problem with +

Hi, Can someone tell me why the first regular expression with the + fails to match the input string? SUN /web>echo cat | grep '+' SUN /web>echo cat | grep '' cat I'm running SunOS 5.10 Thanks. Chris (2 Replies)
Discussion started by: che9000
2 Replies

7. Shell Programming and Scripting

regex problem

Hi, #!/usr/bin/perl -w my $timestamp; my $line = "Fri May 29 18:29:57.357 2009 Morocco Standard Time INFO: pid 3216 tid 1724: 170: 132192: apricocot Native Server: recvd AA_BIN_MSG_VER_CHG"; if ($line =~ /^(.*) INFO: .* recvd AA_BIN_MSG_VER_CHG/) { $timestamp = $1; ... (1 Reply)
Discussion started by: namishtiwari
1 Replies

8. Shell Programming and Scripting

Simple regex problem?

Hi all, I am looking to create words from a sentence which adhere to a custom search pattern from my website: Example: ! +! / += ~ where the terms ! = not, +! = AND NOT, += - and equals and ~ = can be like.... Now here is the issue...i want to split a sentence like the one above on... (1 Reply)
Discussion started by: muay_tb
1 Replies

9. Solaris

gcc 3.4 Regex problem

Hello All, I have been using String.h with gcc 2.95. Now I have upgraded to gcc 3.4. The support for String.h has been removed and I believe Regex.h support is also not inbuilt. So I tried to build my own library for String.h. I had to use Regex.h and rx.h for a succesful library compilation.... (0 Replies)
Discussion started by: manishs13
0 Replies

10. Shell Programming and Scripting

grep regex problem

Hi, I am trying to do something with grep, but for some reason I just can't get it to to work. I am looking for find a match in the second field, the length must be 10 characters and end with 'abc'. The file is in this format: <int><tab><field2> I've tried a few patterns, some work,... (2 Replies)
Discussion started by: iceman
2 Replies

Featured Tech Videos