String processing in shell

Posted on 2006-05-06
Medium Priority
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

Peter Kwan earned 2000 total points
ID: 16621043
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

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

Author Comment

ID: 16626443
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


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

850 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