?
Solved

Search for a string

Posted on 2005-02-27
1
Medium Priority
?
325 Views
Last Modified: 2010-04-20
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
Comment
Question by:manishfromuk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 48

Accepted Solution

by:
Tintin earned 600 total points
ID: 13424008
#!/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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question