What's the easiest way to match a character in a string
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi, I wish to make an if statement matching a letter "D" in a string with no at most 3 characters:
Code:
$string="AHD"
if [ D in $string ]; then
...
fi
Thanks,
Ted
You want the equivalent of what other languages call the function "instr" (and 2 I can think of call "POS")
The instr function returns the position of the search string within the main string.. But, given that it returns zero if the substring is not present - then that will give the answer you want.
So, in SHELL PROGRAMMING the easiest way I know - regardless of the length of the string is;
Code:
string="AHD"
if [ `echo "$string"|grep -s "D"` ]; then
...
fi
Some people prefer "sed", others might use clever manipulation shell parameter expansion. But, most people "know" grep searches for stuff and that echo just echo's stuff and that the pipe connecting the two is saying "echo this and then search it".
So, for that reason - not elegance - it is the simplest and most readable IMHO.
Dave
Note that this is all assuming bash or similar shell. The OP didn't clearly state what language environment he's working in.
Code:
$string="AHD"
This is the wrong syntax for a shell script. You don't precede the declaration of a variable with $, only the expansion of it.
Edit in response to davemguru...
You don't even need the test brackets. if checks the exit status of the final command run, so you can use grep alone:
Code:
if grep -q "D" <<<"$string" ;then
if echo "$string" | grep -q "D" ; then
if echo "$string" | grep "D" >/dev/null ; then
If your version of grep supports the -q option you can use one of the first two options. The first one uses a bash here string rather than a pipe to avoid opening up a subshell.
On systems without -q, use a redirection to dump the output away.
Last edited by David the H.; 11-18-2011 at 11:03 AM.
Reason: as stated
Yeah, I recognized your intent. I just commented because it's often good to give alternatives for different conditions, and to point out that test is not the only command you can use in most branching/looping constructs.
OTOH, I just noticed that you included the -s option in your command, which the gnu grep manpage states behaved like the -q option in some implementations. It also goes on to point out that if you truly need it to be portable, use redirection instead of either option.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.