LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-19-2009, 07:55 AM   #1
prpersonal
LQ Newbie
 
Registered: Oct 2007
Posts: 7

Rep: Reputation: 0
Extract a substring


Hi All,
I am a newbie and not sure if this is the right fourm to ask.
II just have an string "builder_test_string1ABC001_test" and I would like to extract ABC001 from this. Please note that 001 is dynamic and it can change to any numerical value. I wanted to do this in a shell script.


Thanks for ur help in advance
Ramya Srinivaas
 
Old 10-19-2009, 07:59 AM   #2
Lordandmaker
Member
 
Registered: Sep 2005
Location: London, UK
Distribution: Debian
Posts: 258

Rep: Reputation: 39
What is the pattern of the bits you want to keep or destroy? Do you always want three capital letters followed by three numbers? Are they always the only capital letters? Is it always the last six characters you want? Do you always want the substring beginning ABC and finishing at the next underscore? We'd need more sample data to offer any specific help, and we'd *really* like to see what you've tried so far, or even just how you intend to approach this.

You'll likely want to look at sed, awk, and regular expressions. There are a couple of bash scripting guides on www.tldp.org which are mostly invaluable if you've any bash script writing to do.
 
Old 10-19-2009, 08:14 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741
At tldp, start with the Bash Guide for Beginners.

There is also "grep -o" which returns only the matched expression (but only once per line).

In constructing the regular expression, we also need to know if "any numerical value" means any number of digits---i.e. do you need to match "ABC34", ABC289057", etc.
 
Old 10-19-2009, 08:15 AM   #4
prpersonal
LQ Newbie
 
Registered: Oct 2007
Posts: 7

Original Poster
Rep: Reputation: 0
Hi thanks for ur reply. Actually the mentioned string is an output of an clearcase command. I am writing a automatic script which creates a directory called "ABCxxx". All other data in the string is static and will not change. And also this ABC can be in small case also. I always wanted the substring starting from ABC and finishing at the next underscore.

Ex:
String: builder_project1_projectabc023_build
And I always wanted to keep abc023

Thanks
Ramya Srinivaas
 
Old 10-19-2009, 09:16 AM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,281

Rep: Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916
Code:
echo builder_project1_projectabc023_build | grep -o abc[000-999]
 
Old 10-19-2009, 10:06 AM   #6
rn_
Member
 
Registered: Jun 2009
Location: Orlando, FL, USA
Distribution: Suse, Redhat
Posts: 127
Blog Entries: 1

Rep: Reputation: 25
Quote:
Originally Posted by schneidz View Post
grep -o
learned something new. Thanks.
 
Old 10-19-2009, 11:29 AM   #7
prpersonal
LQ Newbie
 
Registered: Oct 2007
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks schneidz, but it didn't worked with:
echo builder_project1_projectabc023_build | grep -o abc[000-999]

but worked with a small correction:
echo builder_project1_projectabc023_build | grep -o abc[0-9][0-9][0-9]

Anyway thanks for all your help
 
Old 10-19-2009, 11:52 AM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741
That does not match your original requirements (capital ABC, everything up to the dash) -- it will match only if there are exactly 3 digits

try this:
Code:
grep -o '[Aa][Bb][Cc][[:digit:]]*_' fil|sed 's/_//'
Edit: "fil" is the name of the file I was using for testing..

Last edited by pixellany; 10-19-2009 at 02:50 PM. Reason: clarification
 
Old 10-19-2009, 02:44 PM   #9
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,281

Rep: Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916
^ good corrections. i shouldve warned that mines was quick-and-dirty.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
extract substring from string in C baddah Programming 6 02-02-2010 04:22 AM
extract substring using sed and regular expressions (regexp) lindylex Programming 20 12-22-2009 10:41 AM
Extract substring matching a regular expression tikit Linux - General 2 02-18-2008 01:47 PM
php preg_replace substring of a substring senyahnoj Programming 5 12-08-2006 11:31 AM
to get a substring dimah Programming 1 06-22-2006 06:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration