Searching a string for a character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching a string for a character
# 1  
Old 11-29-2004
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  
Old 11-29-2004
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! Smilie

Cheers
ZB
# 3  
Old 11-29-2004
Thanks for the help.
# 4  
Old 01-10-2006
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  
Old 01-10-2006
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  
Old 01-11-2006
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  
Old 01-11-2006
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..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

2. Shell Programming and Scripting

Searching invalid character in list of client name

Hi Friend, I have a client name list and client name has some invalid character due to which some issue raised and list of client are15k. I want to make script who find invalid character name. can you please help me how i can make script, i means i need logic. Valid character are :- ... (5 Replies)
Discussion started by: pallvi_mahajan
5 Replies

3. Shell Programming and Scripting

Searching Alphanumeric Character From a File

Hi, In a error log file, the error code of a particular error contains both Alphabet and Numbers. My problem statement is to find the error codes from a particular log. That means, I need to search a word, which contains both alphabet and number. Please help me. Below is two examples of error... (1 Reply)
Discussion started by: snehasish_jana
1 Replies

4. UNIX for Dummies Questions & Answers

Exclude new line character while searching

Hi, I have a log file and want to search "0.48976360737438795" with excluding new line character. Currently i am using the following command: cat LogFile.log | tr -d '\n' | grep '0.48976360737438795' This command giving me the response if file size is small but if i am using a big file... (11 Replies)
Discussion started by: ankit jain
11 Replies

5. Shell Programming and Scripting

Awk: Searching for length of words between slash character

Dear UNIX Community, I have a set of file paths like the one below: \\folder name \ folder1 \ folder2 \ folder3 \ folder4 \\folder name \ very long folder name \ even longer name I would like to find the length of the characters (including space) between the \'s. However, I want... (6 Replies)
Discussion started by: vnayak
6 Replies

6. Shell Programming and Scripting

searching the required string and appending string to it.

Hi all, I have some data in the form of adc|nvhs|nahssn|njadnk|nkfds in the above data i need to write a script so thet it will append "|||" to the third occurnace in the string ..... the outout should look like adc|nvhs|nahssn||||njadnk|nkfds Thanks, Firestar. (6 Replies)
Discussion started by: firestar
6 Replies

7. UNIX for Dummies Questions & Answers

Searching a particular character in a file

Hi, I want to search how many times is "a" present in a file named "data". How to do this? Please help (4 Replies)
Discussion started by: gautamshaw
4 Replies

8. Shell Programming and Scripting

Korn: How to loop through a string character by character

If I have a string defined as: MyString=abcde echo $MyString How can I loop through it character by character? I haven't been able to find a way to index the string so that I loop through it. shew01 (10 Replies)
Discussion started by: shew01
10 Replies

9. Shell Programming and Scripting

Extracting a string from one file and searching the same string in other files

Hi, Need to extract a string from one file and search the same in other files. Ex: I have file1 of hundred lines with no delimiters not even space. I have 3 more files. I should get 1 to 10 characters say substring from each line of file1 and search that string in rest of the files and get... (1 Reply)
Discussion started by: mohancrr
1 Replies

10. UNIX for Dummies Questions & Answers

searching for $ character

I need to search in text files for the $ character. I tried putting it in double quotes and single quotes . ie grep "$" filename but unix finds everything in the file. Whats the best way to do this? thanks (4 Replies)
Discussion started by: MizzGail
4 Replies
Login or Register to Ask a Question