• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 332
  • Last Modified:

Search for a string

I want to search a string withing another string in  a unix shell script.

e.g

string1='cat dog mouse'
string2='cat dog mouse rabbit'

there should be a loop for string 1 and it should search for all strings in string 2 and it should display me the string not found in string1 e.g rabbit should be shown.

Thanks

Manish
0
manishfromuk
Asked:
manishfromuk
1 Solution
 
TintinCommented:
#!/bin/sh
string1='cat dog mouse'
string2='cat dog mouse rabbit'

for word in $string2
do
  echo "$string1" | grep -q $word  || echo $word
done
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now