Find the answer to your Linux question:
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.
  1. #1
    Just 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

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    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.

  3. #3
    Just Joined!
    Join Date
    Aug 2004
    Posts
    42
    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
    Here is the code I wrote, but I am gettingt the following error.

    [: ./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

  4. #4
    Just 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.

    Code:
    typeset words=0
    while read str
    do
    	typeset i=0
    	while &#91; $i -lt 9 &#93;
    	do
    		count&#91;$i&#93;=0
    		let i=i+1
    	done
    	typeset flag=1
    	i=0
    	let j=i+1
    	while &#91; $i -lt 64 &#93; && &#91; $&#123;str&#58;$i&#58;$j&#125;!="\0" &#93; && &#91; $&#123;str&#58;$i&#58;$j&#125;!="\n" &#93;
    	do
    		if &#91; $&#123;str&#58;$i&#58;$j&#125;!="c" &#93; && &#91; $&#123;str&#58;$i&#58;$j&#125;!="o" &#93; && &#91; $&#123;str&#58;$i&#58;$j&#125;!="m" &#93; && &#91; $&#123;str&#58;$i&#58;$j&#125;!="p" &#93; && &#91; $&#123;str&#58;$i&#58;$j&#125;!="i" &#93; && &#91; $&#123;str&#58;$i&#58;$j&#125;!="l" &#93; && &#91; $&#123;str&#58;$i&#58;$j&#125;!="a" &#93; && &#91; $&#123;str&#58;$i&#58;$j&#125;!="t" &#93; && &#91; $&#123;str&#58;$i&#58;$j&#125;!="n" &#93;
    		then
    			flag=0
    			break	
    		fi
    		let i=i+1
    		let j=i+1
    	done
    
    	if &#91; $flag==0 &#93;
    	then
    		continue
    	fi
    
    	i=0
    	let j=j+1
    	while &#91; $i -lt 64 &#93; && &#91; $&#123;str&#58;$i&#58;$j&#125;!="\0" &#93; && &#91; $&#123;str&#58;$i&#58;$j&#125;!="\n" &#93;
    	do
    		if &#91; $&#123;str&#58;$i&#58;$j&#125;==c &#93; 
    		then let count&#91;0&#93;=count&#91;0&#93;+1
    		fi
    		let i=i+1
    		let j=i+1
    	done
    
    	i=0
    	let j=j+1
    	while &#91; $i -lt 64 &#93; && &#91; $&#123;str&#58;$i&#58;$j&#125;!="\0" &#93; && &#91; $&#123;str&#58;$i&#58;$j&#125;!="\n" &#93;
    	do
    		if &#91; $&#123;str&#58;$i&#58;$j&#125;==o &#93; 
    		then let count&#91;1&#93;=count&#91;1&#93;+1
    		fi
    		let i=i+1
    		let j=i+1
    	done
    
    	i=0
    	let j=j+1
    	while &#91; $i -lt 64 &#93; && &#91; $&#123;str&#58;$i&#58;$j&#125;!="\0" &#93; && &#91; $&#123;str&#58;$i&#58;$j&#125;!="\n" &#93;
    	do
    		if &#91; $&#123;str&#58;$i&#58;$j&#125;==m &#93; 
    		then let count&#91;2&#93;=count&#91;2&#93;+1
    		fi
    		let i=i+1
    		let j=i+1
    	done
    
    	i=0
    	let j=j+1
    	while &#91; $i -lt 64 &#93; && &#91; $&#123;str&#58;$i&#58;$j&#125;!="\0" &#93; && &#91; $&#123;str&#58;$i&#58;$j&#125;!="\n" &#93;
    	do
    		if &#91; $&#123;str&#58;$i&#58;$j&#125;==p &#93; 
    		then let count&#91;3&#93;=count&#91;3&#93;+1
    		fi
    		let i=i+1
    		let j=i+1
    	done
    
    	i=0
    	let j=j+1
    	while &#91; $i -lt 64 &#93; && &#91; $&#123;str&#58;$i&#58;$j&#125;!="\0" &#93; && &#91; $&#123;str&#58;$i&#58;$j&#125;!="\n" &#93;
    	do
    		if &#91; $&#123;str&#58;$i&#58;$j&#125;==i &#93; 
    		then let count&#91;4&#93;=count&#91;4&#93;+1
    		fi
    		let i=i+1
    		let j=i+1
    	done
    
    	i=0
    	let j=j+1
    	while &#91; $i -lt 64 &#93; && &#91; $&#123;str&#58;$i&#58;$j&#125;!="\0" &#93; && &#91; $&#123;str&#58;$i&#58;$j&#125;!="\n" &#93;
    	do
    		if &#91; $&#123;str&#58;$i&#58;$j&#125;==l &#93; 
    		then let count&#91;5&#93;=count&#91;5&#93;+1
    		fi
    		let i=i+1
    		let j=i+1
    	done
    
    	i=0
    	let j=j+1
    	while &#91; $i -lt 64 &#93; && &#91; $&#123;str&#58;$i&#58;$j&#125;!="\0" &#93; && &#91; $&#123;str&#58;$i&#58;$j&#125;!="\n" &#93;
    	do
    		if &#91; $&#123;str&#58;$i&#58;$j&#125;==a &#93; 
    		then let count&#91;6&#93;=count&#91;6&#93;+1
    		fi
    		let i=i+1
    		let j=i+1
    	done
    
    	i=0
    	let j=j+1
    	while &#91; $i -lt 64 &#93; && &#91; $&#123;str&#58;$i&#58;$j&#125;!="\0" &#93; && &#91; $&#123;str&#58;$i&#58;$j&#125;!="\n" &#93;
    	do
    		if &#91; $&#123;str&#58;$i&#58;$j&#125;==t &#93; 
    		then let count&#91;7&#93;=count&#91;7&#93;+1
    		fi
    		let i=i+1
    		let j=i+1
    	done
    
    	i=0
    	let j=j+1
    	while &#91; $i -lt 64 &#93; && &#91; -n $&#123;str&#58;$i&#58;$j&#125; &#93; && &#91; $&#123;str&#58;$i&#58;$j&#125;!="\n" &#93;
    	do
    		if &#91; $&#123;str&#58;$i&#58;$j&#125;==n &#93; 
    		then let count&#91;8&#93;=count&#91;8&#93;+1
    		fi
    		let i=i+1
    		let j=i+1
    	done
    
    	i=0
    	j=1
    	while &#91; $i -lt 9 &#93;
    	do
    		if &#91; $i!=1 &#93; && &#91; $i!=4 &#93; #&& &#91; $&#123;count&#58;$i&#58;$j&#125; -gt 1 &#93;
    		then flag=0;
    		fi
    		let i=i+1
    		let j=i+1
    	done
    	
    	if &#91; $count&#91;1&#93; > 2 &#93; && &#91; $count&#91;4&#93; > 2 &#93;
    	then flag=0
    	fi
    
    	if &#91; $&#123;#str&#125; > 2 &#93;
    	then flag=0
    	fi
    
    	if &#91; $flag==1 &#93;
    	then echo $str
    	fi
    
    done < words.txt
    Could someone give me tips on why this code is not working.

  5. #5
    Just 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.

  6. #6
    Linux Guru bigtomrodney's Avatar
    Join Date
    Nov 2004
    Location
    Ireland
    Posts
    6,133
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •