Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

sed a substring out of a string

sed a substring out of a string

sed a substring out of a string

Hi I am not sure if I am in the right forum.

I have a quick concern with the sed command.

I am trying to extract a substring from a string in shell script.

This is my code so far:


cmdRun="-vs LAST_BUS_DAY=20111005"
time_stamp=`echo "$cmdRun" | sed 's/.*([0-9])+.*/\\\\1/'`
echo $time_stamp

I just need to extract 20111005.  If this case the numbers are in the end.  But in some other cases it might be in the middle of the string.  Therefore, I chose to use sed.  

However, when I run this code, It match the entire $cmdRun string.  Please advice!


RE: sed a substring out of a string


   This is a good forum.

   I used sed in a makefile a long time ago to replace footer text on a website.  I forget how I did it.  I read the man page at the time.  I think sed has one of those "In A Nutshell" books about an inch thick.

   I use perl to solve problems like this.  The switch command is an extremely powerful search and replace tool.

   Your operation above, is looking for a consecutive series of numbers, right?



RE: sed a substring out of a string

Yes my operation is looking for 8 consecutive numbers.   

RE: sed a substring out of a string


   So, you are searching for sequences of eight numbers, and rejecting any sequence with more or less than eight of them?

   Your command line above selects one character on the basis being from 0 to 9.  I don't understand set very well, but I see nothing that forces an eight character sequence.

   You have a programming problem, easily solved in C.  Probably, it is easily solved in perl, but I would have to read my book on it.


RE: sed a substring out of a string

with sed I think is a little more difficult because sed matches the complete line. Why not use awk?. I show you an example:

cmdRun="-vs LAST_BUS_DAY=20111005"
time_stamp=`echo "$cmdRun" | awk '{print (substr($0,match($0,"(19|20)[0-9][0-9](0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])"),8))}'`
echo $time_stamp

The regexp matches a valid date 19xx or 20xx folowed by a valid month 0x or 10 or 11 or 12 followed by a day 0x or 1x or 2x or 30 or 31.

The substr command extract the 8 digits beginning where the match command matches the date.

The output from my terminal was:

$ cmdRun="-vs LAST_BUS_DAY=20111005"

$ time_stamp=`echo "$cmdRun" | awk '{print (substr($0,match($0,"(19|20)[0-9][0-9](0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])"),8))}'`

$ echo $time_stamp

The time_stamp line is all a single line.

Hope this helps.

RE: sed a substring out of a string


   I am actually not sure about sed, but I know there is an "in a Netshell" book on awk, and it is very thick indeed.  If the OP has any programming skills, they should know C.  Perl is worth learning.  I cannot see myself trying to figure out awk.  


RE: sed a substring out of a string

Hi JHG, I understand you, but if you need work with filters in an easy way, awk is a very good choice.

The man page about awk is only two or three pages long, there is a port to windows also.

Buy in any case is up to you, if you don't like it don't use it.


RE: sed a substring out of a string

awk is not as hard to learn as sed.
A perquisite for either is a grasp of regex (which I am just a novice in)
sed is much more regex dependent than awk.
regex expressions for just about anything can be found using google.

For example this:


from regexlib matches 20000101 and checks for incorrect leap year dates.

www.engtran.com  www.niswug.org
Phenom IIx6 1100T = 8GB = FX1400 = XP64SP2 = SW2009SP3
"Node news is good news."

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Low-Volume Rapid Injection Molding With 3D Printed Molds
Learn methods and guidelines for using stereolithography (SLA) 3D printed molds in the injection molding process to lower costs and lead time. Discover how this hybrid manufacturing process enables on-demand mold fabrication to quickly produce small batches of thermoplastic parts. Download Now
Design for Additive Manufacturing (DfAM)
Examine how the principles of DfAM upend many of the long-standing rules around manufacturability - allowing engineers and designers to place a part’s function at the center of their design considerations. Download Now
Taking Control of Engineering Documents
This ebook covers tips for creating and managing workflows, security best practices and protection of intellectual property, Cloud vs. on-premise software solutions, CAD file management, compliance, and more. Download Now

Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close