LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 11-18-2011, 12:49 AM   #1
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 431
Blog Entries: 32

Rep: Reputation: 3
What's the easiest way to match a character in a string


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
 
Old 11-18-2011, 01:29 AM   #2
davemguru
Member
 
Registered: Apr 2006
Location: London
Distribution: Pclos,Debian,Puppy,Fedora
Posts: 87

Rep: Reputation: 42
Quote:
Originally Posted by ted_chou12 View Post
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
 
Old 11-18-2011, 06:54 AM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Code:
if [[ $string = *D* ]]
then
    # the string contains "D"
fi
 
Old 11-18-2011, 06:57 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Or
Code:
if [[ $string =~ D ]]
then
  echo D found in string
fi
 
1 members found this post helpful.
Old 11-18-2011, 10:50 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
One more, just for fun:

Code:
if [[ ${string//[^D]} ]]; then
	echo "D found in string"
fi
string manipulation

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
 
Old 11-19-2011, 06:56 AM   #6
davemguru
Member
 
Registered: Apr 2006
Location: London
Distribution: Pclos,Debian,Puppy,Fedora
Posts: 87

Rep: Reputation: 42
In response to "David the H"
Yes, unfortunately I am showing my age. My solution was based on the lowest common denominator - viz: bourne shell.
 
Old 11-20-2011, 12:33 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ boost on Linux : \w can't match Chinese/Japanese character cnsung Programming 0 05-06-2010 05:00 AM
What is the easiest way to match a mount point to a device? worm5252 Linux - Newbie 3 12-28-2009 06:49 PM
reg expr: match a string A-Z, a-z, * or a blank ( but string can not be all blanks) matt007 Programming 4 12-22-2009 08:55 AM
sed regular expression match everything up to a certain character bhepdogg Programming 3 05-28-2009 02:59 PM
awk regexp for one character match nemobluesix Linux - General 7 02-16-2009 10:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration