Choose a single word from a wordlist by matching the letters it contains?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Choose a single word from a wordlist by matching the letters it contains?
# 22  
Old 03-14-2010
without the need of using grep or hashes in the perl code:-

Code:
perl -lne 'BEGIN{$l=join "", sort split //,shift} $w=join "",sort split // ;
print if $w eq $l' '41a2b3cd' wordlist

SmilieSmilieSmilie
# 23  
Old 03-14-2010
Quote:
Originally Posted by ahmad.diab
without the need of using grep or hashes in the perl code:-

Code:
perl -lne 'BEGIN{$l=join "", sort split //,shift} $w=join "",sort split // ;
print if $w eq $l' '41a2b3cd' wordlist

SmilieSmilieSmilie
Note that the grep part allowed for repeated characters on both sides (pattern - record).
Try your code with a record like mmacintos against a pattern like nniomscta Smilie

Code:
% print mmacintos | perl -lne 'BEGIN{$l=join "", sort split //,shift} $w=join "",sort split // ; 
print if $w eq $l' 'nniomscta' 
%

Code:
% print mmacintos | perl -nle'BEGIN {                                                            
  $l = join "", sort grep !$_{$_}++ , split //, shift;                                                       
  }
%_ = (); $w = join "", sort grep !$_{$_}++ , split //;
print if $w eq $l;
' 'nniomscta' 
mmacintos

Actually, that was dennis.jacob's original idea.
# 24  
Old 03-14-2010
Here are some ways to do it with grep/egrep. I haven't followed all the posts so this may not address all the requirements. Just another approach.
Code:
egrep -w '[niomscta]{8}' file

(8 is the string length. Here it is in a script and testing):

# cat ./t2
#! /bin/sh
count=$(echo -n $1 | wc -m) # is there a shorter way to character count an argument?
egrep -w [$1]{$count} file
# ./t2 smo     
# ./t2 smoa
maso
# ./t2 niomsct
# ./t2 niomscta
monastic

Here's a loop with grep that you could use like "script.sh n i o m s c t a". It greps the first argument then greps the second from that output, etc, until it runs out of arguments. Not perfect, and not as good as egrep.
Code:
#! /bin/sh
count=$#
output=$(cat file)
while [ $count -ne 0 ]
do                     
output=$(echo "$output" | grep "$1")
shift
count=$(expr "$count" - 1)
done
echo "$output"

# 25  
Old 03-15-2010
Quote:
Originally Posted by radoulov
Note that the grep part allowed for repeated characters on both sides (pattern - record).
Try your code with a record like mmacintos against a pattern like nniomscta Smilie

Code:
% print mmacintos | perl -lne 'BEGIN{$l=join "", sort split //,shift} $w=join "",sort split // ; 
print if $w eq $l' 'nniomscta' 
%

Code:
% print mmacintos | perl -nle'BEGIN {                                                            
  $l = join "", sort grep !$_{$_}++ , split //, shift;                                                       
  }
%_ = (); $w = join "", sort grep !$_{$_}++ , split //;
print if $w eq $l;
' 'nniomscta' 
mmacintos

Actually, that was dennis.jacob's original idea.
you are right sorry missed that but in below modified code is still more faster and simple from using hashes and grep; isn't it?


Code:
perl -lne 'BEGIN{$l=join "", sort split //,shift ; $l =~ tr/a-zA-Z0-9//s }
$w=join "",sort split // ;$w =~ tr/a-zA-Z0-9//s; print if $w eq $l ;' 'nniomscta' wordlist

SmilieSmilieSmilie

Last edited by ahmad.diab; 03-15-2010 at 05:38 AM..
# 26  
Old 03-15-2010
Quote:
Originally Posted by ahmad.diab
you are right sorry missed that but in below modified code is still more faster and simple from using hashes and grep;


Well, you may still need to adjust the character classes to include all possible ranges, but I agree that in this case (single characters), tr is definitely more appropriate.

Quote:
isn't it?
[...]
Why? Is this some sort of competition Smilie
# 27  
Old 03-15-2010
Quote:
Originally Posted by radoulov


Well, you may still need to adjust the character classes to include all possible ranges, but I agree that in this case (single characters), tr is definitely more appropriate.

Why? Is this some sort of competition Smilie
yes you are right just add option c to tr as modified tr/A-Za-z0-9//cs and it will do the job (for char like ";").

and really radoulov I love perl after I had seen your codes...I hope that some day I can compete with you.SmilieSmilieSmilie
# 28  
Old 03-15-2010
Quote:
Originally Posted by ahmad.diab
yes you are right just add option c to tr as modified tr/A-Za-z0-9//cs and it will do the job (for char like ";").
Hm,
the c modifier will complement / invert the matches (so it will squeeze only characters not found in the search list). Am I missing something?

Quote:
and really radoulov I love perl after I had seen your codes...I hope that some day I can compete with you.SmilieSmilieSmilie
You're already doing it Smilie But bear in mind that I'm quite new to Perl, so you'd better find someone with better skills Smilie

Anyway, I must admit that the tr idea was really great and far better than mine.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Performance assessment of using single or combined pattern matching

Hi, I want to know which pattern matching technique will be giving better performance and quick result. I will be having the patterns in a file and want to read that patterns and search through a whole file of say 70 MB size. whether if i initially create a pattern matching string while... (7 Replies)
Discussion started by: ananan
7 Replies

2. Shell Programming and Scripting

Bash directory loop, but only choose those folders with specific word in it

Hello, how in bash i can get directory loop, but only choose those folders with specific word in it, so it will only echo those with specific word #!/bin/bash for filename in /home/test/* do if ; then echo $filename; fithx! (4 Replies)
Discussion started by: ZerO13
4 Replies

3. Shell Programming and Scripting

Creating single pattern for matching multiple files.

Hi friends, I have a some files in a directory. for example 856-abc 856-def 851-abc 945-def 956-abc 852-abc i want to display only those files whose name starts with 856* 945* and 851* using a single pattern. i.e 856-abc 856-def 851-abc 945-def the rest of the two files... (2 Replies)
Discussion started by: Little
2 Replies

4. Shell Programming and Scripting

Word matching and write other data

Hi all, I have 7 words CAD CD HT RA T1D T2D BD Now I have 1 file which contain data in large number of rows and columns from 2 nd column onwards it contain any of these 7 words or may be more than one words among above 7 words: these 7 names are present in starting from 2nd ... (7 Replies)
Discussion started by: manigrover
7 Replies

5. Shell Programming and Scripting

Matching single quote in a regular expression

I trying to match the begining of the following line in a perl script with a regular expression. $ENV{'ORACLE_HOME'} I tried this regluar expession: /\$ENV\{\'ORACLE_HOME\'\}/ Instead of match, I got a blank prompt > It seems to be a problem with the single quote. If I take it... (11 Replies)
Discussion started by: JC9672
11 Replies

6. Shell Programming and Scripting

Extract single word from lines

Hi I have a huge line like the following: this word and many other words AA122134 other and other words and AA224466 and other other other other word AA667210 words I only want extract the words AA?????? and put them in a column, how could i do ? thx so much! (7 Replies)
Discussion started by: AdminLew
7 Replies

7. UNIX for Dummies Questions & Answers

grep only word matching the pattern

Hi gurus, A file contains many words in format "ABC.XXXX.XXXX.X.GET.LOG" (X->varying). Now my shell script want this list (only words in formatABC.XXXX.XXXX.X.GET.LOG ) to continue the process. Pls help me. Thanks, Poova. (8 Replies)
Discussion started by: poova
8 Replies

8. UNIX for Dummies Questions & Answers

Print last 2 letters in a word

Hi, I have a file which has the below information tm.orbit72 tm.orbit12 tm.orbit78 tm.orbitye I want to print the last two letters in the above file. Please let me know how can i do that. (6 Replies)
Discussion started by: Krrishv
6 Replies

9. Shell Programming and Scripting

Matching multiples of a single character using sed and awk

Hi, I have a file 'imei_01.txt' having the following contents: $ cat imei_01.txt a123456 bbr22135 yet223 where I want to check whether the expression 'first single alphabet followed by 6 digits' is present in the file (here it is the first record 'a123456') I am using the following... (5 Replies)
Discussion started by: royalibrahim
5 Replies

10. Shell Programming and Scripting

matching a letter in a word

hi, if i have a string of letters and seperatly i have a single letter. how do i check whether that specific letter is in my string aswell? any ideas? (2 Replies)
Discussion started by: Furqan_79
2 Replies
Login or Register to Ask a Question