LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 09-21-2006, 03:00 PM   #1
eur0dad
Member
 
Registered: Jul 2006
Posts: 57

Rep: Reputation: 15
Need Help Extracting A Substring


I've got a string ABC-DEF-0.1.2-3.4 and I'm trying to extract the "DEF" part. Which command should I look into for doing this?
 
Old 09-21-2006, 03:10 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
there are plenty of tools, like cut, sed etc.. but as you've not indicated what is special about this substring we can't really say that much more... is it that it's the text between the first and second hypen? if so then cut would be my preferred option.
 
Old 09-21-2006, 03:13 PM   #3
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
What language are you using to do this? (bash, perl, etc?) Do you have regular expression parsing available to you? Are you looking for the 5th, 6th and 7th characters in the string, or are you looking for "DEF", wherever they may be?
A little more information and we'll try to help all we can
 
Old 09-21-2006, 03:26 PM   #4
eur0dad
Member
 
Registered: Jul 2006
Posts: 57

Original Poster
Rep: Reputation: 15
Sorry, I should've been more specific. I'm simply trying to extract the string between ABC- and -0.1.2-3.4 using BASH (the method needs to be as generic as possible, so using the standard tools is probably a better idea). DEF string will only contain letters, ABC- is fixed, and -0.1.2-3.4 will vary, but format should be similar.
 
Old 09-21-2006, 03:41 PM   #5
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
acid's got the right of it, then:

Code:
[560:me@server01 ~/work]$ echo "ABC-DEF-0.1.2-3.4" > shtuff.txt
[561:me@server01 ~/work]$ cat shtuff.txt | cut -b 5-7
DEF
[562:me@server01 ~/work]$
[563:me@server01 ~/work]$ WOWAVARIABLE="ABC-DEF-0.1.2-3.4"
[564:me@server01 ~/work]$ echo $WOWAVARIABLE | cut -b 5-7
DEF
[565:me@server01 ~/work]$
That basically cuts whatever's in the file or variable between characters 5 and 7, inclusive. It will work with multiple lines by default - the examples shown here might be useful as well

Cheers!

Last edited by rose_bud4201; 09-21-2006 at 03:42 PM.
 
Old 09-21-2006, 03:48 PM   #6
eur0dad
Member
 
Registered: Jul 2006
Posts: 57

Original Poster
Rep: Reputation: 15
This seemed to work too: cut -f2 -d"-"

Thanks for the help, guys!
 
Old 09-21-2006, 10:34 PM   #7
johnsanty
Member
 
Registered: Apr 2006
Posts: 49

Rep: Reputation: 15
Quote:
Originally Posted by eur0dad
I've got a string ABC-DEF-0.1.2-3.4 and I'm trying to extract the "DEF" part. Which command should I look into for doing this?
Bro check this site... I hope it provides you an anwer.
http://www.tldp.net/LDP/abs/html/special-chars.html
 
  


Reply



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
replace a substring with another string in C zeppelin Programming 21 11-09-2009 09:59 PM
to get a substring dimah Programming 1 06-22-2006 06:31 AM
how many occurances of a substring are they n a string linuxmandrake Programming 1 04-11-2006 02:02 AM
Replace substring with SED marri Programming 2 07-09-2005 05:18 PM

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

All times are GMT -5. The time now is 07:08 AM.

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
Open Source Consulting | Domain Registration