String processing in shell

Posted on 2006-05-06
Last Modified: 2008-02-01
How do I process search/replace strings (something like using substr in perl) in a shell script (bash, ksh, etc.). Here's what I want to do:
   - Given a string, I want to:
          a) find a substring
          b) extract the previous 8B in front of it

For example (spaces added only to flag substring and desired match string; there will be no spaces in the "real" string), given the string (hex characters only):
          AB 24BDF5ABED8CA734 09C8A78348C87A6B DFE2334ADAB342097A87F6DE7A5B78C678EF876AB
   I will be given a substring (this will be known): 09C8A78348C87A6B
   and want to extract the previous 8 bytes (16 characters - 24BDF5ABED8CA734) using only shell commands (this is the unknown I wish to find).

Can this be done? I've tried using
    expr index "$string" $substring
to no avail... the first of these three expressions shows the most promise, but the "$substring" matches only by character and not the entire substring - e.g. 0 or 9 or C or 8 or A ...etc. will match. I can do this in Perl, but my environment requires I only use shell.
Question by:mikestan
    LVL 16

    Accepted Solution

    You may use awk to do the work for you:

    STR=<your string>
    echo $STR | awk '{
       s=index($0, "09C8A78348C87A6B");
       if (s != 0) {
          print substr($0, s-16, 16);
    LVL 45

    Expert Comment

    $ echo AB24BDF5ABED8CA73409C8A78348C87A6BDFE2334ADAB342097A87F6DE7A5B78C678EF876AB | sed 's:.*\(................\)09C8A78348C87A6B.*:\1:'
    LVL 1

    Author Comment

    Thanks to you both! I was also trying something like this but also thought of making the "09C8A78348C87A6B" into a shell variable (i.e. known=09C8A78348C87A6B read in from a file or user prompt). This will work for now and will figure out how to program in more advanced features later.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now