Pull a string out of a string in a shell script

I have a linux shell script that contains a variable with a very long string in it.  I need to pull a string out of that very long string.  The string will start with "oct 19" and end with "blahblah".  But the number of characters in between will vary.  How do I extract the string?
serjoshAsked:
Who is Participating?
 
TintinConnect With a Mentor Commented:
Simply do

grep -Eo "oct 19.*blahblah" file
0
 
Tyler LaczkoCommented:
$mystring = "long string oct aklsdfgklajg akgjasdgjka blah asdgasdg";
//split into 2 parts
@s_array1 = split("oct", $mystring);
split into 2 parts
@s_array2 = split("oct", @s_array1[1]);

print @s_array2[0];
0
 
Tyler LaczkoCommented:
$mystring = "long string oct aklsdfgklajg akgjasdgjka blah asdgasdg";
//split into 2 parts
@s_array1 = split("oct 19", $mystring);
split into 2 parts
@s_array2 = split("blahblah", @s_array1[1]);

print @s_array2[0];
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Tyler LaczkoCommented:
$mystring = "long string oct aklsdfgklajg akgjasdgjka blah asdgasdg";
//split into 2 parts
@s_array1 = split("oct 19", $mystring);
//split into 2 parts
@s_array2 = split("blahblah", @s_array1[1]);

print @s_array2[0];
0
 
serjoshAuthor Commented:
Professionalcomputersolutions,
I get all kinds of errors in my shell script after I put your code into it.

./Cgrep.sh: line 11: =: command not found
./Cgrep.sh: line 12: //split: No such file or directory
./Cgrep.sh: line 13: syntax error near unexpected token `('
./Cgrep.sh: line 13: `@s_array1 = split("oct 19", $mystring);'
0
 
Tyler LaczkoCommented:
lol

dont just copy and paste my code.

look at it and read it then write your own code.

mine is just an algorithm

Declare your long string
Split the string into 2 parts
you will now have 2 strings 1st is garbage 2nd is the start of what you want to the very end
split the 2nd string into 2 parts
you will have 2 more strings the first contains the info you want the second is garbage

use the string in pos 0 of the second array.
0
 
serjoshAuthor Commented:
I guess the 'split' function is the part that is confusing me.  This is a bash shell script.  Can the unix split command be used the way you are saying?  It seems it's used to split files not strings?  I get errors every time i try to use it.  perhaps you could give me a little more verbose description of how I can accomplish this?  Sorry for my confusion =P
0
 
TintinCommented:
professionalcomputersolutions gave you Perl code, not shell script.

Just use my grep solution.  Much easier :)
0
 
serjoshAuthor Commented:
That seems to have done it, thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.