The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 05:35 AM
how to know if a string contains a certain pattern Deanne Shell Programming and Scripting 11 08-09-2007 08:19 PM
String compare sbasetty Shell Programming and Scripting 14 02-07-2007 05:24 AM
Compare Char to String Phobos High Level Programming 3 04-09-2005 11:01 AM
string compare gundu Shell Programming and Scripting 3 03-23-2005 04:42 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-25-2008
jerryte jerryte is offline
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
  #2 (permalink)  
Old 01-25-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,643
One way
Code:
echo "$var" | grep -q 'Item'
if [ $? -eq 0 ] ; then
  echo"found it"
fi
  #3 (permalink)  
Old 01-25-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 699
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
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 04:34 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0