![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
One way
Code:
echo "$var" | grep -q 'Item' if [ $? -eq 0 ] ; then echo"found it" fi |
|
#3
|
||||
|
||||
|
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
Code:
% ./s1 Found it. 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
Code:
% ./s2 Found it (expected). |
||||
| Google The UNIX and Linux Forums |