Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search for a string in string 2

Status
Not open for further replies.

Sarah1977

Programmer
Sep 9, 2004
5
BE
could somebody please let me know how to find a string inside a string?

I'm trying to do the following:

InputString= "teststring,MSG1=Unix.scripting,MSG2=teststring.ext"
SubString = string between ",MSG1=" and ",MSG2=" = in this example = "Unix.scripting"

I really don't find a way to extract this substring.

 
Code:
echo "teststring,MSG1=Unix.scripting,MSG2=teststring.ext" | sed -e '/MSG1=/s/MSG1=\([^,][^,]*\).*/\1/g'

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Using bash?
You can use simple,sed style, subs to extract using
syntax like:
Code:
InputString="teststring,MSG1=Unix.scripting,MSG2=teststring.ext"
outstring=${InputString/*MSG1=/""}
outstring=${outstring/,MSG2*/""}
echo $outstring
 
Soory vlad, didn't see you. Vlad's is cooler.
Not that I'm a sed slack myself ;)
 
It depends on which shell you are using. Korn shell (see man pages) allows "pattern matching" using # or ## or % or %% to do this. Or you could try to echo the string through a pipe into a sed command (or an awk command).

I hope that helps.

Mike
 
Sorry Guys,

In the time it takes to type, get interrupted and type again, you have already answered !

Best wishes.

Mike
 
Hi Guys,

Nice atmosphere here!

a little tuning required. With the code of <vgersh99> I get more then I require. I get "teststring,Unix.scripting" and I should only get "Unix.scripting".

A better alternative to me would be that you explain what these complex sed command arguments do, but a faster way would be that you just change it?!

The choice is all yours!
Best regards,

Sarah (Belgium)
 
sorry 'bount that.

Code:
echo "teststring,MSG1=Unix.scripting,MSG2=teststring.ext" | sed -e '/MSG1=/s/.*MSG1=\([^,][^,]*\).*/\1/g'

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Hi Vlad,

What is wrong with this script?

FILENAME="NU1=001,TX4=Test.Test,Test,DA1=23-11-2003,VID=R0001,FID=00002.001"
FIELD1=",TX4="
FIELD2=",DA1="
FIELDVALUE='echo "$FILENAME" | sed -e '/"$FIELD1"/s/.*"$FIELD1"\([^"$FIELD2"][^"$FIELD2"]*\).*/\1/g'
echo "$FILENAME"
echo "$FIELD1"
echo "$FIELD2"
echo "$FIELDVALUE"

As your code was working, I think t terminated on comma's, but I also would like to catch the comma's inside the value. So from the above code we should get "Test.Test,Test" as FIELDVALUE.
 
ok, how about:

Code:
echo "teststring,MSG1=Unix.scripting,MSG2=teststring.ext" | sed -e '/MSG1=/s/.*MSG1=\([^=][^=]*\).*/\1/g;s/,[^,][^,]*$//g'


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Hi Vlad,

Is there really no way to stop at ",DA1="??
The reason why I need to capture that value is because I need to replace some special characters by their Decimal value (This functionality is already tested and working). but this means that the Field value can contain anything (@,;.etc), but it is very unlikely that it will contain ",DA1=".

Please advise.
Best regards,

Sarah
 
If sed doesn't work use awk...
Code:
FILENAME="NU1=001,TX4=Test.Test,Test,DA1=23-11-2003,VID=R0001,FID=00002.001"
FIELD1=",TX4="
FIELD2=",DA1="
FIELDVALUE=$(echo $FILENAME | awk -v FS="\\($FIELD1|$FIELD2\\)" '{print $2}')
echo $FIELDVALUE
Test.Test,Test
 
I don't understand the question. The posted suggestion should do what you've been trying to do. Does it?

If not, pls provide a sample input and a desired output.


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thank you all who contributed to the solution!
The code of <Ygor> did what I needed.
Also I would like to thank <vgersh> for the valuable help!
 
Another sed way:
FIELDVALUE=$(echo "$FILENAME" | sed "s!.*$FIELD1!!;s!$FIELD2.*!!")

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top