The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
compare string in two files MiLKTea Shell Programming and Scripting 2 03-10-2008 02:35 AM
how to know if a string contains a certain pattern Deanne Shell Programming and Scripting 11 08-09-2007 05:19 PM
String compare sbasetty Shell Programming and Scripting 14 02-07-2007 02:24 AM
Compare Char to String Phobos High Level Programming 3 04-09-2005 08:01 AM
string compare gundu Shell Programming and Scripting 3 03-23-2005 01:42 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-25-2008
Registered User
 

Join Date: Jan 2008
Posts: 1
Compare string to a pattern

I am new to unix and need to learn how to compare a variable $subject to a string pattern. If the variable has the word "Item" in it then it should be true. How do I do this? Currently I am using the Bourne shell but I can also use Korn or Bash.

I come from a Rexx background where strings are easy:
IF WORDPOS('Item', subject) > 0 THEN
SAY 'Found it'
END
Reply With Quote
Forum Sponsor
  #2  
Old 01-25-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
One way
Code:
echo "$var" | grep -q 'Item'
if [ $? -eq 0 ] ; then
  echo"found it"
fi
Reply With Quote
  #3  
Old 01-25-2008
drl's Avatar
drl drl is offline
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 556
Hi.

Make you feel at home:
Code:
#!/usr/bin/env rexx

/*
# @(#) s1       Demonstrate Linux rexx.
*/

subject = 'We are looking for an item in a line.'

If WORDPOS('item', subject) > 0 Then
  SAY 'Found it.'
Else
  SAY ' Cannot see item.'

exit 0
Producing:
Code:
% ./s1
Found it.
Make me feel at home:
Code:
#!/bin/bash -

# @(#) s1       Demonstrate rexx function emulation.

debug="echo"
debug=":"

wordpos() {
  local phrase="$1" string="$2"
  $debug " wordpos, looking for $phrase in $string"
  if [[ $string == *$phrase* ]]
  then
    return 0
  else
    return 1
  fi
}

if wordpos item "Jack and Jill"
then
  echo " Found it (unexpected!)."
fi

if wordpos item "Now here is an item embedded."
then
  echo " Found it (expected)."
fi

exit 0
Producing:
Code:
% ./s2
 Found it (expected).
The key to s2 is not the function, of course, it is the syntax of and in the if statement. The page at http://www.tldp.org/LDP/abs/html/index.html is long, but valuable... cheers, drl
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:25 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0