Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Tuesday, July 8, 2008

Shell:Bash:replace substring in string

${string/substring/replacement} Replace first match of $substring with $replacement
${string//substring/replacement} Replace all matches of $substring with $replacement
${string/#substring/replacement} If $substring matches front end of $string, substitute $replacement for $substring
${string/%substring/replacement} If $substring matches back end of $string, substitute $replacement for $substring


Refer to the link for more information.

Friday, July 20, 2007

Linux:shell:grep how to use grep's result

mystr="IloveChina"
echo $mystr |grep China
echo $? #this will return 0
echo $mystr |grep USA
echo $? #this will return 1
#so you could use this returned value to justify if grep found the string.
# Or you use how many searched results returned to do this, for example,
echo $mystr |grep -c China # return 1 to you
echo $mystr |grep -c USA # return 0 to you
# so simply, you can use the number to justify if you found the results like
if [ `
echo $mystr |grep -c China` -ge 1 ]; then
echo "Yes, you are"
fi




Locations of visitors to this page