Solved

Regular Expression:  get third field and only single instances

Posted on 2004-04-22
2
286 Views
Last Modified: 2012-05-04
I need a   Regular Expression to add to the following

$mediaServerName = ($storageUnit =~

that will pull put the third line and place only single instances of the same field into the array. The $storageUnit  looks like:


STK9310_L00_DB00D02 2 db00d02 1 0 6 1 0 "*NULL*" 0 8 2000 *NULL*  
STK9310_L00_DB00M01 2 db00m01 1 0 6 4 0 "*NULL*" 0 2 2000 *NULL*  
STK9310_L00_DB00M01 2 db00m01 1 0 6 1 0 "*NULL*" 0 8 2000 *NULL*  

and I would like for the array to have db00d02 and db00m01.


NOT: db00d02 ,db00m01 and db00m01
0
Comment
Question by:MatthewF
2 Comments
 
LVL 7

Expert Comment

by:leflon
ID: 10887750
Hi MatthewF,
you can use a hash to ensure there is only a single instance

%hsh;
while(<>) {
  @line =split;
  if ( not $hsh{$line[0]}) {
    $hsh{$line[0]} = $line[2];
}

now you can traverse the hash and do what you like with it (store it in an array, print it etc.)

hth
leflon
0
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 10889554
$storageUnit = <<ENDHERE;
STK9310_L00_DB00D02 2 db00d02 1 0 6 1 0 "*NULL*" 0 8 2000 *NULL*  
STK9310_L00_DB00M01 2 db00m01 1 0 6 4 0 "*NULL*" 0 2 2000 *NULL*  
STK9310_L00_DB00M01 2 db00m01 1 0 6 1 0 "*NULL*" 0 8 2000 *NULL*
ENDHERE
my %seen=(''=>1);
@array = grep{!$seen{$_}++}map{(split)[2]}split/\n/, $storageUnit ;
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question