Hi,
i need to check my string1 is in another string2.
input_filename=017200910.DC30025
if [ my file has .DC3 ]
then
i need do some action
Can anyone tell me how to check my string is in another string.
example
i need to check .DC3 is in 017200910.DC30025
filename=1234.DC300
echo $filename | grep -q "\.DC3"
if [ $? -eq 0 ]; then
echo "found match"
fi
If using bash/ksh, you can also use this:
input_filename=017200910.DC30025
if [ $(echo $input_filename | grep \.DC3) ]; then
# take necessary action
fi
vino
October 10, 2007, 8:19am
4
Using shell builtins
[/tmp]$ name=abc.DC3qwe
[/tmp]$ if [[ $name = *.DC3* ]] ; then echo "name=[$name]"; fi
name=[abc.DC3qwe]
[/tmp]$ name=abcxDC3qwe
[/tmp]$ if [[ $name = *.DC3* ]] ; then echo "name=[$name]"; fi
[/tmp]$
Vino's suggestion works with ksh as well. Suggest using that. Not invoking external programs can boost your script's speed quite a bit.
Otherwise:
% input_filename=017200910.DC30025
% case $input_filename in (*.DC3*) echo "do some action here...";;esac
do some action here...