Getting equivalent values using regular expression


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting equivalent values using regular expression
# 1  
Old 03-24-2009
Getting equivalent values using regular expression

Hi,

I have data (seperated by "\n") like this:

Code:
MN  - Regular expressions are a syntax, implemented in Perl

and certain other environments, making it not only possible but easy to do 

some of the following.

ID - 12,13

BA - Character classes are alternative single characters within square 

brackets, and are not to be confused with OOP classes, which are blueprints 

for objects.If not used carefully, they can yield unexpected results.

I want to match and retrieve the contents of MN and BA.

I tried like this:

Code:
if($str=~/MN\s+\-(.*)/)
{
   print "\n$1\n";
}
#This prints only part of the string (Regular expressions are a syntax, implemented in Perl)


If i try like this:

if($str=~/MN\s+\-(.*)\nID/)
{
   print "\n$1\n";
}
#Its not working!!!

The output should be like this:
Code:
Regular expressions are a syntax, implemented in Perl and certain other environments, making it not only possible but easy to do some of the following

Character classes are alternative single characters within square brackets, and are not to be confused with OOP classes, which are blueprints for objects.If not used carefully, they can yield unexpected results

what should i change in order to get that???

Regards
vanitha
# 2  
Old 03-24-2009
Perl regex don't work across multiple lines (well, actually they do, if you use the m modifier to your regex, but even then only if all the lines are in the same variable). You have to first concatenate your lines by your own logic and then search for the stuff you want.

Code:
#!/usr/bin/perl -W

use strict;
use warnings;

my $line;
my %lines;
my $id;
while ( $line = <> ) {
    chomp $line;
    if ( $line =~ /^(\w{2})\s+-\s+/ ) {
        $id = $1;
        $line =~ /^\w{2}\s+-\s+(.*)/;
        $line = $1;
        $lines{$id} = "";
    }
    $lines{$id} .= $line . " ";
}
print $lines{"MN"}, "\n";
print $lines{"BA"}, "\n";

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

2. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

3. Shell Programming and Scripting

regular expression

Could anyone of you please guide me on making correct regular expression to match the exact word or character I have some numbers like 12345780 extn 1234 1234567 x 43545 13245678 Extn 454857 if * ]]; then VAR3=`echo "$NUMBER" | nawk -F "*" '{print $1 $2}'` ... (4 Replies)
Discussion started by: nram_krishna@ya
4 Replies

4. Shell Programming and Scripting

Integer expression expected: with regular expression

CA_RELEASE has a value of 6. I need to check if that this is a numeric value. if not error. source $CA_VERSION_DATA if * ] then echo "CA_RELESE $CA_RELEASE is invalid" exit -1 fi + source /etc/ncgl/ca_version_data ++ CA_PRODUCT_ID=samxts ++ CA_RELEASE=6 ++ CA_WEEK_NO=7 ++... (3 Replies)
Discussion started by: ketkee1985
3 Replies

5. Shell Programming and Scripting

Regular expression matching in BASH (equivalent of =~ in Perl)

In Perl I can write a condition that evaluates a match expression like this: if ($foo =~ /^bar/) { do blah blah blah } How do I write this in shell? What I need to know is what operator do I use? The '=~' doesn't seem to fit. I've tried different operators, I browsed the man page for... (3 Replies)
Discussion started by: indiana_tas
3 Replies

6. Shell Programming and Scripting

Regular expression

Hi I have to extract the first field and the last %field of the following out put.. /home (/abc/def/bhd ) : 522328 total allocated Kb 319448 free allocated Kb ... (2 Replies)
Discussion started by: Harikrishna
2 Replies

7. Linux

Regular expression to extract "y" from "abc/x.y.z" .... i need regular expression

Regular expression to extract "y" from "abc/x.y.z" (2 Replies)
Discussion started by: rag84dec
2 Replies

8. UNIX for Dummies Questions & Answers

Regular expression

Hello, I have a string of the form " &x.y.z" I would like to grep all the lines when "in" can be found in either x or y. How to write the corresponding regular expression ? I have tried the following but it does not work: grep -i " *&.*(in)*.*.*(in)*.*" Any ideas ? Thank you Max (1 Reply)
Discussion started by: maxvirrozeito
1 Replies

9. Shell Programming and Scripting

regular expression help

hello all.. I'm a bit new to this site.. and I hope to learn alot.. but I've been having a hard time figuring this out. I'm horrible with regular expressions.. so any help would be greatly appreciated. I have a file with a list of names like this: LASTNAME, FIRSTNAME, MIDDLEINITIAL how can... (5 Replies)
Discussion started by: mac2118
5 Replies

10. Shell Programming and Scripting

Regular Expression + Aritmetical Expression

Is it possible to combine a regular expression with a aritmetical expression? For example, taking a 8-numbers caracter sequece and casting each output of a grep, comparing to a constant. THX! (2 Replies)
Discussion started by: Z0mby
2 Replies
Login or Register to Ask a Question