search in text for string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search in text for string
# 1  
Old 10-24-2010
search in text for string

search in text for string

I assigned text to variable
for example :
cafe tea coca > drinks
and i want to check is $i is in "drink" or not


thanks
# 2  
Old 10-24-2010
What shell are you using (e.g. bash, ksh, csh, zsh)?

Can you confirm if "drink" is a file or a another shell text variable.
# 3  
Old 10-24-2010
my shell is bash



---------- Post updated at 05:34 PM ---------- Previous update was at 05:31 PM ----------




and "drink" is another shell text variable
I assigned via ">"
cafe tea coca > drink
# 4  
Old 10-24-2010
you could use bash replace something like this:

Code:
drink="cafe tea coca"
[ "${drink}" = "${drink/tea/}" ] || echo "Drink contains tea"

Above will work for ksh93 or bash, Basically we are saying if $drink equals $(replace "tea" with nothing in drink) is false echo "

or use bash in-built compare

Code:
[[ "$drink" =~ "tea" ]] && echo "Drink contains tea"


Last edited by Chubler_XL; 10-24-2010 at 07:58 PM.. Reason: explain what is happening
This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 10-24-2010
Code:
#!/bin/bash

drink="cafe tea cocoa"
echo $drink | grep -q $1

if [[ $? == 0 ]]; then
    echo "Match is Found"
fi

This User Gave Thanks to soleil4716 For This Post:
# 6  
Old 10-24-2010
bash code:
  1. #!/bin/bash
  2. i=tea
  3. drinks="cafe tea cocoa"
  4. if [[ $drinks = *$i* ]]; then
  5.   echo yup
  6. fi

shell code:
  1. i=tea
  2. drinks="cafe tea cocoa"
  3. case $drinks in
  4.   *$i*) echo yup
  5. esac
* No external programs died during the making of this code

Last edited by Scrutinizer; 10-24-2010 at 08:45 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 10-24-2010
Not the most elegant one but still working...

Code:
( grep $i <(echo "$drink") >/dev/null 2>&1 ) && echo 'yup !'

This User Gave Thanks to ctsgnb For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

2. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

3. UNIX for Dummies Questions & Answers

Search String, Out matched text and input text for no match.

I need to search a string for some specific text which is no big deal using grep. My problem is when the search fails to find the text. I need to add text like "na" when my search does not match. I have tried this command but it does not work when I put the command in a loop in a bash script: ... (12 Replies)
Discussion started by: jojojmac5
12 Replies

4. Shell Programming and Scripting

To Search for a string and to extract the string from the text

Hi Team I have an huge xml where i need to search for a ceratin numbers. For example 2014-05-06 15:15:41,498 INFO WebContainer : 10 CommonServicesLogs - CleansingTriggerService.invokeCleansingService Entered PUBSUB NOTIFY MESSAGE () - <?xml version="1.0" encoding="UTF-8"... (5 Replies)
Discussion started by: Kannannair
5 Replies

5. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

6. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

7. Shell Programming and Scripting

Search a string and append text after the string

Hi, I have a file like this... <o t="Batch" id="8410" p="/" g="32"> <a n="name"> <v s="DBBA1MM"/> </a> <a n="owner"> <v r="/Administrator"/> </a> <a n="rights"> <v s="95"/> </a> <a n="debugLevel"> <v s="3"/> </a> <a n="avsStoreLoc"> <v... (8 Replies)
Discussion started by: kesu2k
8 Replies

8. AIX

search text string

hi, i know this trick: find / -name '*' -exec grep 'string to search' {} \; but it won't show the path/file where it came from. how can i make the path/file to show up. thanks in advance, itik (2 Replies)
Discussion started by: itik
2 Replies

9. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

10. Shell Programming and Scripting

appending string to text file based on search string

Hi, I need to append string "Hi" to the beginning of the lines containing some specific string. How can I achieve that? Please help. Malay (1 Reply)
Discussion started by: malaymaru
1 Replies
Login or Register to Ask a Question