Results 1 to 6 of 6
Hello,
Can someone please tell me how to write a bash or shell script for the following questions:
Given a word such as compilation , the subword problem can be ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 09-29-2005 #1Just Joined!
- Join Date
- Aug 2004
- Posts
- 42
shell script: find subwords with 'compilation' word
Hello,
Can someone please tell me how to write a bash or shell script for the following questions:
Given a word such as compilation, the subword problem can be described as follows: list all of the words which can be created, using only letters in the given word. For example subwords of compilation word include clap, and mop. It would also contain the subword lotion, because there were two o's available to use in the word. In general, if a word has more the two i's or more than two o's or more than one repetition of any other letter of compilation it cannot be a subword.
I have a dictionery with 1000 of words in it, and I need to find all the subword in the compilation.
Only thing that I know is the I need to use grep command with regular expression in it. But I am not quite familiar with regular expression.
Any help would much much appreicatied.
Thanks
Suresh
- 09-29-2005 #2
This seems like a homework question. As per the LinuxForums rules, we do not answer homework questions.
Try it out on your own, and then, when you have a specific problem or question, ask it.
- 10-04-2005 #3Just Joined!
- Join Date
- Aug 2004
- Posts
- 42
Here is the code I wrote, but I am gettingt the following error.Code:typeset words=0 while read str do typeset i=0 while [ $i -lt 9 ] do count[$i]=0 let i=i+1 done typeset flag=1 i=0 let j=i+1 while [ $i -lt 64 ] && [ -n ${str:$i:$j} ] && [ ${str:$i:$j}!='\n'] do if [ ${str:$i:$j}!=c ] && [ ${str:$i:$j}!=o ] && [ ${str:$i:$j}!=m ] && [ ${str:$i:$j}!=p ] && [ ${str:$i:$j}!=i ] && [ ${str:$i:$j}!=l ] && [ ${str:$i:$j}!=a ] && [ ${str:$i:$j}!=t ] && [ ${str:$i:$j}!=n ] then flag=0 break fi let i=i+1 let j=i+1 done if [ $flag==0 ] then continue fi i=0 let j=j+1 while [ $i -lt 64 ] && [ ${str:$i:$j}!='\0' ] && [ ${str:$i:$j}!='\n' ] do if [ ${str:$i:$j}==c ] then let count[0]=count[0]+1 fi let i=i+1 let j=i+1 done i=0 let j=j+1 while [ $i -lt 64 ] && [ ${str:$i:$j}!='\0' ] && [ ${str:$i:$j}!='\n' ] do if [ ${str:$i:$j}==o ] then let count[1]=count[1]+1 fi let i=i+1 let j=i+1 done i=0 let j=j+1 while [ $i -lt 64 ] && [ ${str:$i:$j}!='\0' ] && [ ${str:$i:$j}!='\n' ] do if [ ${str:$i:$j}==m ] then let count[2]=count[2]+1 fi let i=i+1 let j=i+1 done i=0 let j=j+1 while [ $i -lt 64 ] && [ ${str:$i:$j}!='\0' ] && [ ${str:$i:$j}!='\n' ] do if [ ${str:$i:$j}==p ] then let count[3]=count[3]+1 fi let i=i+1 let j=i+1 done i=0 let j=j+1 while [ $i -lt 64 ] && [ ${str:$i:$j}!='\0' ] && [ ${str:$i:$j}!='\n' ] do if [ ${str:$i:$j}==i ] then let count[4]=count[4]+1 fi let i=i+1 let j=i+1 done i=0 let j=j+1 while [ $i -lt 64 ] && [ ${str:$i:$j}!='\0' ] && [ ${str:$i:$j}!='\n' ] do if [ ${str:$i:$j}==l ] then let count[5]=count[5]+1 fi let i=i+1 let j=i+1 done i=0 let j=j+1 while [ $i -lt 64 ] && [ ${str:$i:$j}!='\0' ] && [ ${str:$i:$j}!='\n' ] do if [ ${str:$i:$j}==a ] then let count[6]=count[6]+1 fi let i=i+1 let j=i+1 done i=0 let j=j+1 while [ $i -lt 64 ] && [ ${str:$i:$j}!='\0' ] && [ ${str:$i:$j}!='\n' ] do if [ ${str:$i:$j}==t ] then let count[7]=count[7]+1 fi let i=i+1 let j=i+1 done i=0 let j=j+1 while [ $i -lt 64 ] && [ ${str:$i:$j}!='\0' ] && [ ${str:$i:$j}!='\n' ] do if [ ${str:$i:$j}==n ] then let count[8]=count[8]+1 fi let i=i+1 let j=i+1 done i=0 j=1 while [ $i -lt 9 ] do if [ $i!=1 ] && [ $i!=4 ] && [ ${count:$i:$j} -gt 1 ] then flag=0; fi let i=i+1 let j=i+1 done if [ ${count:1:2} -gt 2 ] && [ ${count:4:5} -gt 2 ] then flag=0 fi if [ ${#str} -gt 2 ] then flag=0 fi if [ $flag==1 ] then echo $str fi done < words.txt
[: ./test.sh 152: unbalanced []
where 152 is the line of the code "done < words.txt"
Can someone please tell me where I am making a mistake?
thanks
suresh
- 10-04-2005 #4Just Joined!
- Join Date
- Aug 2004
- Posts
- 42
The error was a syntax error (missed a space along with ][).
But the code does not produce any output. The words.txt contains a dictionary of allwords. This is the code.
Could someone give me tips on why this code is not working.Code:typeset words=0 while read str do typeset i=0 while [ $i -lt 9 ] do count[$i]=0 let i=i+1 done typeset flag=1 i=0 let j=i+1 while [ $i -lt 64 ] && [ ${str:$i:$j}!="\0" ] && [ ${str:$i:$j}!="\n" ] do if [ ${str:$i:$j}!="c" ] && [ ${str:$i:$j}!="o" ] && [ ${str:$i:$j}!="m" ] && [ ${str:$i:$j}!="p" ] && [ ${str:$i:$j}!="i" ] && [ ${str:$i:$j}!="l" ] && [ ${str:$i:$j}!="a" ] && [ ${str:$i:$j}!="t" ] && [ ${str:$i:$j}!="n" ] then flag=0 break fi let i=i+1 let j=i+1 done if [ $flag==0 ] then continue fi i=0 let j=j+1 while [ $i -lt 64 ] && [ ${str:$i:$j}!="\0" ] && [ ${str:$i:$j}!="\n" ] do if [ ${str:$i:$j}==c ] then let count[0]=count[0]+1 fi let i=i+1 let j=i+1 done i=0 let j=j+1 while [ $i -lt 64 ] && [ ${str:$i:$j}!="\0" ] && [ ${str:$i:$j}!="\n" ] do if [ ${str:$i:$j}==o ] then let count[1]=count[1]+1 fi let i=i+1 let j=i+1 done i=0 let j=j+1 while [ $i -lt 64 ] && [ ${str:$i:$j}!="\0" ] && [ ${str:$i:$j}!="\n" ] do if [ ${str:$i:$j}==m ] then let count[2]=count[2]+1 fi let i=i+1 let j=i+1 done i=0 let j=j+1 while [ $i -lt 64 ] && [ ${str:$i:$j}!="\0" ] && [ ${str:$i:$j}!="\n" ] do if [ ${str:$i:$j}==p ] then let count[3]=count[3]+1 fi let i=i+1 let j=i+1 done i=0 let j=j+1 while [ $i -lt 64 ] && [ ${str:$i:$j}!="\0" ] && [ ${str:$i:$j}!="\n" ] do if [ ${str:$i:$j}==i ] then let count[4]=count[4]+1 fi let i=i+1 let j=i+1 done i=0 let j=j+1 while [ $i -lt 64 ] && [ ${str:$i:$j}!="\0" ] && [ ${str:$i:$j}!="\n" ] do if [ ${str:$i:$j}==l ] then let count[5]=count[5]+1 fi let i=i+1 let j=i+1 done i=0 let j=j+1 while [ $i -lt 64 ] && [ ${str:$i:$j}!="\0" ] && [ ${str:$i:$j}!="\n" ] do if [ ${str:$i:$j}==a ] then let count[6]=count[6]+1 fi let i=i+1 let j=i+1 done i=0 let j=j+1 while [ $i -lt 64 ] && [ ${str:$i:$j}!="\0" ] && [ ${str:$i:$j}!="\n" ] do if [ ${str:$i:$j}==t ] then let count[7]=count[7]+1 fi let i=i+1 let j=i+1 done i=0 let j=j+1 while [ $i -lt 64 ] && [ -n ${str:$i:$j} ] && [ ${str:$i:$j}!="\n" ] do if [ ${str:$i:$j}==n ] then let count[8]=count[8]+1 fi let i=i+1 let j=i+1 done i=0 j=1 while [ $i -lt 9 ] do if [ $i!=1 ] && [ $i!=4 ] #&& [ ${count:$i:$j} -gt 1 ] then flag=0; fi let i=i+1 let j=i+1 done if [ $count[1] > 2 ] && [ $count[4] > 2 ] then flag=0 fi if [ ${#str} > 2 ] then flag=0 fi if [ $flag==1 ] then echo $str fi done < words.txt
- 10-04-2005 #5Just Joined!
- Join Date
- Jun 2005
- Location
- Canada, Halifax
- Posts
- 86
Instead of bothering us here, why don't you ask whomever you got the code from in the first place? It's obvious form your "question" that you didn't write it.
- 10-04-2005 #6Linux Guru
- Join Date
- Nov 2004
- Posts
- 6,110
You seem to be assigning all of your variables and operating on them, but not dumping them out. I would have expected to see a lot of echo commands in each loop with piping out to files. I only see input from the words.txt but you should have something going the other way. I would recommend using echo at the end of each loop and piping contents of the entire script to an outfile. tee might be useful for you here.


Reply With Quote
