The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Korn: How to loop through a string character by character shew01 Shell Programming and Scripting 10 12-02-2008 07:58 AM
Extracting a string from one file and searching the same string in other files mohancrr Shell Programming and Scripting 1 09-19-2007 03:17 AM
converting character string to hex string axes High Level Programming 5 09-20-2006 01:04 PM
searching for $ character MizzGail UNIX for Dummies Questions & Answers 4 01-05-2006 11:21 PM
searching by string length GADO UNIX for Dummies Questions & Answers 4 11-14-2005 06:00 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-29-2004
turbulence's Avatar
turbulence turbulence is offline
Registered User
  
 

Join Date: Nov 2004
Posts: 10
Searching a string for a character

I have a co-worker that is trying to make a Wheel of Fortune game and he wants to know if there is a way to search a string for the letter given by the user.
  #2 (permalink)  
Old 11-29-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Code:
echo "string" | grep 'x' >/dev/null 2>&1
if [ "$?" -eq "0" ]; then
  echo "Found x in string"
else
  echo "Couldnt find it"
fi
You get the idea....

Check out my hangman shell script - you can see the technique employed.

I wish I had a job like your co-worker!

Cheers
ZB
  #3 (permalink)  
Old 11-29-2004
turbulence's Avatar
turbulence turbulence is offline
Registered User
  
 

Join Date: Nov 2004
Posts: 10
Thanks for the help.
  #4 (permalink)  
Old 01-10-2006
manthasirisha manthasirisha is offline
Registered User
  
 

Join Date: Jan 2006
Posts: 59
Zazzybob,

I had the same probelm as turbulence's co-worker...and ur solution really worked.. but can u please explain the idea behind ur solution?
i didnt get what is the significance of /dev/null there in the code?


thanks
sirisha
  #5 (permalink)  
Old 01-10-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Quote:
Originally Posted by manthasirisha
i didnt get what is the significance of /dev/null there in the code?
Grep, in its simplest form, has this habit of shouting out loud, about the things it discovered. And if it did find, then the exit status is 0. Else 1.

In the OP's case, he wants only detect the presence of a letter in a string. So whatever grep outputs, be it success or failure, discard it.

Here is a non-grep solution as well

Code:
[~/temp]$ echo $LANG
en_US.UTF-8
[~/temp]$ [[ "$LANG" == *US* ]] && echo "US LANG" || echo "LOCALE"
US LANG
[~/temp]$
  #6 (permalink)  
Old 01-11-2006
manthasirisha manthasirisha is offline
Registered User
  
 

Join Date: Jan 2006
Posts: 59
vino,

i am afraid if i understand what u suggested. Your explantion of how grep works and thereby comparing the exit status of grep to detect the presence of a letter in the string is understood, but i didnt quite follow two aspects:

1. why is there a /dev/null in his piece of code?
2. How are achieving the same without a grep in your solution?

can you please take efforts to elaborate?

Thanks a ton again,
Sirisha
  #7 (permalink)  
Old 01-11-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Quote:
Originally Posted by manthasirisha
1. why is there a /dev/null in his piece of code?
I did mention that if grep finds anything, it will output whatever it found. Together with that, the exit status will be set accordingly.

See this

Code:
[/tmp]$ cat xyz
maroon
pink
yellow
[/tmp]$ grep pink xyz
pink
[/tmp]$ echo $?
0
[/tmp]$ grep pink xyz 1> /dev/null
[/tmp]$ echo $?
0
[/tmp]$ grep pink xyz.file 1> /dev/null
grep: xyz.file: No such file or directory
[/tmp]$ echo $?
2
[/tmp]$ grep pink xyz.file 1> /dev/null 2> /dev/null
[/tmp]$ echo $?
2
[/tmp]$ grep pink xyz 1> /dev/null 2> /dev/null
[/tmp]$ echo $?
0
[/tmp]$
That should explain the role of /dev/null. If not, read it from up the wiki site.

Quote:
Originally Posted by manthasirisha
2. How are achieving the same without a grep in your solution?
The non-grep solution makes use of a shell-builtin.

See this thread - String extraction from user input - sh

vino

Last edited by vino; 01-11-2006 at 08:46 AM..
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 05:25 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0