Perl match multiple numbers from a variable similar to egrep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl match multiple numbers from a variable similar to egrep
# 1  
Old 02-13-2013
Perl match multiple numbers from a variable similar to egrep

I want to match the number exactly from the variable which has multiple numbers seperated by pipe symbol similar to search in egrep.below is the code which i tried
Code:
#!/usr/bin/perl
my $searchnum = $ARGV[0];
my $num = "148|1|0|256";
print $num;
if ($searchnum =~ /$num/)
{
 print "found";
}
else
{
  print "not-found";
 
}
 



Expected output
perl number_match 1
found
perl number_mtach.pl 1256
not found

# 2  
Old 02-13-2013
Code:
#!/usr/bin/perl
my $searchnum = $ARGV[0];
my $num = '(?:148|1|0|256)';
### or use a regex reference - my $num = qr/148|1|0|256/;
print $num;
if ($searchnum =~ /\b$num\b/)
{
 print "found";
}
else
{
  print "not-found";
 
}


Last edited by elixir_sinari; 02-13-2013 at 06:18 AM..
# 3  
Old 02-13-2013
One way:

Code:
#!/usr/bin/perl
use strict;
use warnings;

my $num = "148|1|0|256";
my $inp = $ARGV[0];

my %h1=map{$_ => 1}split(/\|/,$num);

if (exists($h1{$inp})){
        print "Found";
}else{
        print "Not Found";
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Egrep - Only Match First Occurrence

echo 'String#1 and String#2' | egrep -o -m 1 'String#.{1}' String#1 String#2 I'm trying to just match the first occurrence of 'String#' + 1 character. I thought the "-m 1" switch would do that for me. Instead I get both occurrences. Can somebody provide some insight? Thanks! (5 Replies)
Discussion started by: sudo
5 Replies

2. UNIX for Dummies Questions & Answers

egrep getting numbers only

Hello, I am kind of a noob with unix, so i'd like some help. I am trying to get some ip address with an nmap scan and then put from the result of the scan only the ip numbers this is an example Starting Nmap 5.21 ( Nmap - Free Security Scanner For Network Exploration & Security Audits )... (3 Replies)
Discussion started by: AscaL
3 Replies

3. Shell Programming and Scripting

Regex/egrep matching numbers in brackets

Experts: I don't know that regular expressions will ever be easy for me, so if one of you guru's could help out, I'd appreciate it. I'm trying to match a line in our syslog, but I can't figure out how to match a number inside a bracket. This is what I'm trying to match. "Jul 16 00:01:34... (2 Replies)
Discussion started by: jdveencamp
2 Replies

4. Shell Programming and Scripting

Joining multiple files based on one column with different and similar values (shell or perl)

Hi, I have nine files looking similar to file1 & file2 below. File1: 1 ABCA1 1 ABCC8 1 ABR:N 1 ACACB 1 ACAP2 1 ACOT1 1 ACSBG 1 ACTR1 1 ACTRT 1 ADAMT 1 AEN:N 1 AKAP1File2: 1 A4GAL 1 ACTBL 1 ACTL7 (4 Replies)
Discussion started by: seqbiologist
4 Replies

5. Shell Programming and Scripting

Perl - match e-mail addresses in multiple files

Hi, I'm trying to write a script that will check multiple files in a directory (all the relevant filenames begin "TT04.NOTES") for e-mail addresses, and then print these addresses to screen with a count at the bottom. I'm a bit of a novice with Perl but thought it would be the best tool for the... (2 Replies)
Discussion started by: wf1974
2 Replies

6. Shell Programming and Scripting

Perl: Printing Multiple Lines after pattern match

Hello People, Need some assistance/guidance. OUTLINE: Two files (File1 and File2) File1 has some ids such as 009463_3922_1827 897654_8764_5432 File2 has things along the lines of: Query= 009463_3922_1827 length=252 (252 letters) More stufff here ... (5 Replies)
Discussion started by: Deep9000
5 Replies

7. Shell Programming and Scripting

Multiple variable in a variable in Perl

Hi All, I am trying to convert the below Csh while loop into Perl while loop but the problem is that in this csh script, i have 2 variables inside a variable -> $count is a variable {SB$count} as a whole is another variable. Csh is able to assign values to such variable like the below but i do... (3 Replies)
Discussion started by: Raynon
3 Replies

8. Shell Programming and Scripting

Perl: Match a line with multiple search patterns

Hi I'm not very good with the serach patterns and I'd need a sample how to find a line that has multiple patterns. Say I want to find a line that has "abd", "123" and "QWERTY" and there can be any characters or numbers between the serach patterns, I have a file that has thousands of lines and... (10 Replies)
Discussion started by: Juha
10 Replies

9. UNIX for Dummies Questions & Answers

match similar rows. uniq?

hi i have data which is in two columns (such as below). i need to compare two rows against each other and if one row matches the other row (except for different case), and their values in the second column are different, then it prints out one of the rows (either is fine). here is an... (5 Replies)
Discussion started by: Streetrcr
5 Replies

10. Shell Programming and Scripting

read and match multiple lines in perl

Could any one tell me how to read and match multiple lines in perl? Did this code below still work in this situation? while (<FILE>) { if (/ /) { } } Thanks a lot! (5 Replies)
Discussion started by: zx1106
5 Replies
Login or Register to Ask a Question