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?
# 8  
Old 03-13-2010
Nothing to see here Smilie

Last edited by alister; 03-13-2010 at 12:39 PM.. Reason: Incorrect Solution
# 9  
Old 03-13-2010
Quote:
Originally Posted by alister
Code:
while read word; do
    [ -z "$(echo $word | tr -d acimnost)" ] && echo $word
done < /home/alex/Desktop/wordlist.txt

Alister
What is this supposed to do?
Code:
alex@lol-pc:~/Desktop$ ./pip
alex@lol-pc:~/Desktop$

# 10  
Old 03-13-2010
Code:
perl -nle'BEGIN {
  @_{split / /, shift} = (1) x length($ARGV[0]);
  }
keys %_ == grep $_{$_}, split // and print;
' 'n i o m s c t a' wordlist

# 11  
Old 03-13-2010
this works perfectly!!! 10000000000000 thxes!!!!!
# 12  
Old 03-13-2010
Quote:
Originally Posted by hakermania
{ perl -lane 'print if (grep {/\b[niomscta]+\b/} $_); ' file } doesn't really suits me. I want to take words from the wordlist with 8 characters (niomscta=8 chars) that contain ALL the letters : n i o m s c t a
Examples:
atcsmoin
oinmatcs
matcsoin etc...
Your command { perl -lane 'print if (grep {/\b[niomscta]+\b/} $_); ' file }
has this output:
Code:
alex@lol-pc:~$ perl -lane 'print if (grep {/\b[niomscta]+\b/} $_);  ' /home/alex/Desktop/wordlist.txt
montana
monica
macintos
action
tomcat
cannon
tinman
nissan
station
samson
tattoo
cccccc
sonics
cosmos
mission
tintin
moomoo

that doesn't suits me at all. I want to do a kind of unscrimbling (hope I wrote it correctly) but not from all the existing words! Only from the wordlist.txt's words!
I misunderstood. Below one is the corrected one.Smilie

Code:
perl -lane 'print   if (join("",sort (split //)) eq "acimnost");' file

# 13  
Old 03-13-2010
The following verifies that each word contains the same amount of unique characters as are listed in letters. If that requirement is meant, then the word is filtered through tr -d. If the list of unique characters in the word matches, an empty string results and the word is printed.
Code:
#!/bin/sh

letters=niomscta
length=${#letters}

while read word; do
    [ $length -ne $(echo $word | sed 's/\(.\)/\1 /g' | tr -s ' ' '\n' | sort | uniq | wc -l) ] && continue
    [ -z "$(echo $word | tr -d $letters)" ] && echo $word
done < data


Determine a word's unique characters by splitting the word into one character per line, sorting, uniq'ing, reassembling into a string and then comparing to the sorted list of allowed letters.
Code:
#!/bin/sh

# letters must be sorted
letters=acimnost

while read word; do
    uniqchars=$(echo $word | sed 's/\(.\)/\1 /g;y/ /\n/' | sort | uniq | paste -s - | tr -d '\t')
    [ "$letters" = "$uniqchars" ] && echo $word
done < data

These solutions allow allowed letters to appear more than once. This can be easily modified by removing uniq from the pipeline.

Alister

Last edited by alister; 03-13-2010 at 01:23 PM..
# 14  
Old 03-13-2010
Quote:
Originally Posted by alister
[...]These solutions allow allowed letters to appear more than once.
Excellent point!
This should handle it too:
Code:
perl -nle'BEGIN {
  @_{split / /, shift} = (1) x length($ARGV[0]);
  }
keys %_ <= grep $_{$_}, split // and print;
' 'n i o m s c t a' infile



---------- Post updated at 07:17 PM ---------- Previous update was at 06:17 PM ----------

Based on dennis.jacob's and alister's ideas, with the almighty Z-Shell Smilie

Code:
letters=(n i o m s c t a)
letters=${(j::)${(uo)letters}}
  
while read -r; do
  [[ ${(j::)${(ous::)REPLY}} == $letters ]] &&
    print -- $REPLY
done<wordlist


Last edited by radoulov; 03-14-2010 at 05:34 AM..
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