Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Pull a string out of a string in a shell script

Posted on 2008-11-04
9
Medium Priority
?
456 Views
Last Modified: 2013-11-13
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?
0
Comment
Question by:serjosh
  • 4
  • 3
  • 2
9 Comments
 
LVL 10

Expert Comment

by:Tyler Laczko
ID: 22880736
$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
 
LVL 10

Expert Comment

by:Tyler Laczko
ID: 22880743
$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
 
LVL 10

Expert Comment

by:Tyler Laczko
ID: 22880746
$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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:serjosh
ID: 22880858
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
 
LVL 10

Expert Comment

by:Tyler Laczko
ID: 22881168
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
 
LVL 48

Accepted Solution

by:
Tintin earned 2000 total points
ID: 22881202
Simply do

grep -Eo "oct 19.*blahblah" file
0
 

Author Comment

by:serjosh
ID: 22881301
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
 
LVL 48

Expert Comment

by:Tintin
ID: 22881316
professionalcomputersolutions gave you Perl code, not shell script.

Just use my grep solution.  Much easier :)
0
 

Author Closing Comment

by:serjosh
ID: 31513290
That seems to have done it, thanks!
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Introduction to Processes

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question