string in a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting string in a string
# 1  
Old 02-13-2009
string in a string

Hi,
I m working on a solution which wud script the following scenarios but not quite getting to work 100%.

Case1(very simple)
S1: HELLO_WORLD_HOW_ARE_YOU
S2: HELLO
Easy n simple soln wud be : echo "$S1" | grep "$S2"

Case2:
S1:HELLO_WORLD_HOW_ARE_YOU
S2:HELLO_HOW_ARE_YOU
In this case, above soln would not return TRUE though my S2 is indeed part of S1 but S2 is basically not Continuous.
I am looking for a soln which wud throw a 1 in the above case.

Case3:
S1:HELLO_WORLD_HOW_ARE_YOU
S2:HELLO_JOHN or HI_WORLD
Even this shud return a TRUE.

Basically my intention is, even if a particular pattern in S2(need not be the entire string S2) matches somewhere in S1, I shud get a TRUE .
Plz remember, both S1 and S2 are not Fixed length !

Any ideas?.

-Anduzzi
# 2  
Old 02-14-2009
Try to split second argument (with respect to '_' or space)
and grep their occurence within arg1 individually.
The answer can depend on how many splitted words occured in input
(e.g. 50%).
# 3  
Old 02-14-2009
Quote:
Originally Posted by anduzzi
Hi,
I m working on a solution which wud script the following scenarios but not quite getting to work 100%.

Please see the forum rules, note (9):
don't write in cyberchat or cyberpunk style
Quote:
Case1(very simple)
S1: HELLO_WORLD_HOW_ARE_YOU
S2: HELLO
Easy n simple soln wud be : echo "$S1" | grep "$S2"

Do you mean:

Code:
S1=HELLO_WORLD_HOW_ARE_YOU
S2=HELLO
echo "$S1" | grep "$S2"

If so, you don't need grep:

Code:
case $S1 in
  *"$S2"*) true ;;
  *) false ;;
esac

Quote:
Case2:
S1:HELLO_WORLD_HOW_ARE_YOU
S2:HELLO_HOW_ARE_YOU
In this case, above soln would not return TRUE though my S2 is indeed part of S1 but S2 is basically not Continuous.

What do you mean by "not continuous"?
Quote:

I am looking for a soln which wud throw a 1 in the above case.

Do you want it to return true (0) or 1 (false)?
Quote:

Case3:
S1:HELLO_WORLD_HOW_ARE_YOU
S2:HELLO_JOHN or HI_WORLD
Even this shud return a TRUE.
Code:
S1=HELLO_WORLD_HOW_ARE_YOU
S2="HELLO_JOHN"
if echo "$S1" | grep "${S2%%_*}"
then
  echo true
else
  echo false
fi

Or:

Code:
S1=HELLO_WORLD_HOW_ARE_YOU
S2='HELLO_JOHN'
case $S1 in
  *"${S2%%_*}"*) echo true ;;
  *) echo false ;;
esac

Quote:

Basically my intention is, even if a particular pattern in S2(need not be the entire string S2) matches somewhere in S1, I shud get a TRUE .
Plz remember, both S1 and S2 are not Fixed length !

Which "particular pattern" do you want to match? If it's the string preceding the first underscore, use ${S2%%_*} as above.

Last edited by cfajohnson; 02-14-2009 at 10:17 PM..
# 4  
Old 02-16-2009
Hi Johnson,
Thanks for your inputs.
Well, when I mean the sting to be searched for is not continuous, it means the occurence of entire S2 in S1 need not be continuous in lot of cases.
eg:
S1 = HELLO_HOW_ARE_YOU
S2 = HELLO_YOU

The above case should be a true.
and when I say true, it means once the S2 is found of to be part of S1(considering all the scenarios), I need to perform an action else skip my processing and I am not looking for a particular pattern but rather a combination of patterns(both continuous and non-continuous)...hope this gives more clarity....

-Anduzzi
# 5  
Old 02-16-2009
try this

Code:
#! /usr/bin/perl
use warnings;
my $S1 = HExLLO_HOW_ARE_YOU;
my $S2 = HELLO_YU_ARE;

my @s1array = split(/_/,$S1);
my @s2array = split(/_/,$S2);

foreach (@s1array){
                $value1 = $_;
                foreach(@s2array){                        $value2 = $_;                           if ($value1 =~ $value2){
                        print "True\n"; exit; }}}
print "False\n";


Thanks
Ahamed Shameer
netgiz.net -
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

awk string comparison unterminated quoted string andrule of thumb

I have the logic below to look up for matches within the columns between the two files with awk. In the if statement is where the string comparison is attempted with == The issue seems to be with the operands, as 1. when " '${SECTOR}' " -- double quote followed by single quote -- awk matches... (1 Reply)
Discussion started by: deadyetagain
1 Replies

3. 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

4. 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

5. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

6. Shell Programming and Scripting

grep exact string from files and write to filename when string present in file

I am attempting to grep an exact string from a series of files within a directory and append that output to the filename when it is present in the file. I've been after this all day with no luck. Thanks for your help in advance :wall:. (4 Replies)
Discussion started by: JC_1
4 Replies

7. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

8. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

9. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

10. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies
Login or Register to Ask a Question