Perl - Regular Expressions - Match complete word only


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl - Regular Expressions - Match complete word only
# 1  
Old 02-28-2012
Perl - Regular Expressions - Match complete word only

Hi Team,

I have two strings like:
Code:
xxx|yyy|Arizona Cardinals| Tell Cardinals | Cardinals
bbb|Bell Earn, Jr | Bell Earn | Jayhawks | hawks

I have a lookup file which has a set of strings. These need to be removed from above two strings

Lookup file Contents:
Code:
Bell Earn, Jr
hawks
Arizona Cardinals

Final output should be like:
Code:
xxx|yyy| Tell Cardinals | Cardinals
bbb| Bell Earn | Jayhawks

What i am trying to do is, complete word need to be removed. Partial matches should not be removed.
Jayhawks in 2nd string should remain as is ( hawks should not match to Jayhawks )
Cardinals in first string should not be removed.

How can i just remove complete word? I tried to use boundary \b, but this is causing issues with Bell Earn, Jr
Bell Earn, Jr have a comma in it and so only Bell Earn is removed. , Jr are retained which is not i am looking for.
I appreciate your responses.

Last edited by Franklin52; 02-29-2012 at 03:15 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 02-29-2012
Code:
$
$
$ cat data.txt
xxx|yyy|Arizona Cardinals| Tell Cardinals | Cardinals
bbb|Bell Earn, Jr | Bell Earn | Jayhawks | hawks
$
$
$
$ cat lookup.txt
Bell Earn, Jr
hawks
Arizona Cardinals
$
$
$
$ perl -lne 'if ($ARGV eq "lookup.txt") { $x{$_}++ }
             else {print join "|", grep {($m=$_)=~s/^\s*//;
                                         ($n=$m)=~s/\s*$//;
                                         !defined $x{$n}
                                        } split/\|/
             }' lookup.txt data.txt
xxx|yyy| Tell Cardinals | Cardinals
bbb| Bell Earn | Jayhawks
$
$
$

tyler_durden

Last edited by durden_tyler; 02-29-2012 at 01:12 AM..
# 3  
Old 02-29-2012
Code:
[root@hostname dir]# cat lookup_file
Bell Earn, Jr
hawks
Arizona Cardinals
[root@hostname dir]#
[root@hostname dir]# cat input_file_with_piped_strings
xxx|yyy|Arizona Cardinals| Tell Cardinals | Cardinals
bbb|Bell Earn, Jr | Bell Earn | Jayhawks | hawks
[root@hostname dir]#
[root@hostname dir]# perl -ne 'chomp ($line = $_); $line=~ s/(\w)\|/$1 |/g; $line =~ s/\|(\w)/| $1/g;
open F1, "< lookup_file";
for $p (<F1>) { chomp($p); $line =~ s/\| $p\s?//g }
print "$line\n"' input_file_with_piped_strings
xxx | yyy | Tell Cardinals | Cardinals
bbb | Bell Earn | Jayhawks
[root@hostname dir]#
[root@hostname dir]#

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

BASH - Regular Expressions :Looking for one word on multiple lines.

Im looking for a bash solution that will use Regular Expressions (not perl, sed or awk) to check the example data below and then give me a status. which would be just simply Match or Mismatch. SYS PS1 is present. Fan status: Normal Input Voltage status: Normal ... (5 Replies)
Discussion started by: popeye
5 Replies

2. Homework & Coursework Questions

Regular Expression to match files in Perl

Hi Everybody! I need some help with a regular expression in Perl that will match files named messages, but also files named message.1, message.2 and so on. So really I need one that will find messages and messages that might be followed by a period and a digit without matching other files like... (2 Replies)
Discussion started by: Hax0rc1ph3r
2 Replies

3. Shell Programming and Scripting

PERL Regular Expressions

im trying to extract some tags between and in a file..for eg..the file format is I want the and extracted from the file i.e the tags which is present b/w and I have the regex for extracting the tags from the whole file but how to specify my search within the and... (1 Reply)
Discussion started by: rajkrishna89
1 Replies

4. Programming

Which language is best suited for regular expressions perl,python.ruby ?

Hello all, i am in a bit of dilema here. i dont know any thing about perl or python. only know a little bit of awk. now unable to take a decission as to which language to go for. my requirement is building a testing framework.suite which will execute ssytem comands remotely on unix... (2 Replies)
Discussion started by: achak01
2 Replies

5. Shell Programming and Scripting

regular expressions using perl script

i have a set of regular expressions. The words in the regular expression should be used to replace the i/p with hyphens '---'. i need perl script to evaluate these regular expression. the words in the regexes when found in the i/p file should be replaced with hyphens '---'. the set of regular... (3 Replies)
Discussion started by: Sgiri1
3 Replies

6. Shell Programming and Scripting

Regular expressions - Perl

Hello everybody, I am trying to connect from hp-ux to win 2003 using perl's Net::Telnet module. Seeing the examples in couple of web sites, I saw I have to declare a Prompt => Can somebody please tell me what my regular expression should be? The prompt after I log in is: ... login:... (1 Reply)
Discussion started by: whatever
1 Replies

7. Shell Programming and Scripting

perl regular expressions and field search

Hello guys/gals, i am sorry as this is probably very simply but i am slowly learning perl and need to convert some old korn shell scripts. I need to be able to search a file line by line but only match a string at particular location on that line, for example character 20-30. So my file... (4 Replies)
Discussion started by: dynamox
4 Replies

8. Shell Programming and Scripting

Regular Expressions HELP - PERL

Hello, $line=USING (FILE '/TEST1/FILENAME'5000) I want to reterive the value between ' and ) which is 5000 here. i have tried out the following expressions ... Type 1 : $Var1=`sed -e 's/.*\' //' -e 's\).*$/' $line`; Type 2 : $Var1=`echo $line | awk -F"\'" '{print $2}' | awk -F"\\)"... (3 Replies)
Discussion started by: maxmave
3 Replies

9. UNIX for Dummies Questions & Answers

Regular Expressions HELP - PERL

Hello, $line=USING (FILE '/TEST1/FILENAME'5000) I want to reterive the value between ' and ) which is 5000 here. i have tried out the following expressions ... Type 1 : $Var1=`sed -e 's/.*\' //' -e 's\).*$/' $line`; Type 2 : $Var1=`echo $line | awk -F"\'" '{print $2}' | awk -F"\\)"... (1 Reply)
Discussion started by: maxmave
1 Replies

10. Shell Programming and Scripting

Perl regular expressions...

I am writing script that will act like the 'comm' utility. My problem is when trying to read whether the user has entered -123 or -1 or -1...etc. I currently have: if(m/??/g){ print "Good.\n"; } So, this should check for all... (1 Reply)
Discussion started by: DrRo183
1 Replies
Login or Register to Ask a Question