Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 01-11-2008, 07:49 AM   #1
LQ Newbie
Registered: Jan 2008
Location: Belgium
Posts: 4

Rep: Reputation: 0
Question extracting part of a string (sed, bash, ...)

I have a sample string like below (with same formatting but length of the fields and integers may vary):

AtmIf/62 Vpt/3 Uni Addr/39206510001234567890,primary

I need to extract the 39206510001234567890 (length may vary)

I would like to do that using sed and Regular expressions but I can't get it right.
If sed won't do it i would like to use a bash script, awk, etc.

I know how to do it in Perl but this is not possible in this case.

Thanks in advance for your help.
Old 01-11-2008, 07:58 AM   #2
LQ Veteran
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389

Using sed:

sed 's%.*/\(.*\),.*%\1%' infile


echo "AtmIf/62 Vpt/3 Uni Addr/39206510001234567890,primary" | sed 's%.*/\(.*\),.*%\1%'

Hope this helps.
Old 01-11-2008, 08:05 AM   #3
LQ Newbie
Registered: Jan 2008
Location: Belgium
Posts: 4

Original Poster
Rep: Reputation: 0
This helps indeed.
Thank you druuna.
Old 01-11-2008, 08:14 AM   #4
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
If the string is always longer than 2 numbers, then the regex is simple. Using GREP, this works:

(Assumes the desired string is always at least 5 numbers---"num" is the file I generated to hold your example)

grep -o '[0-9]\{5,\}' num
The regex translates to "5 or more instances of any digit". The "o" flag tells grep to return only the matched pattern.

This can also be done with SED, but probably will be messier


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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
C: Extracting part of a string trevorv Programming 3 08-29-2007 04:36 PM
String extracting / string operation Xeratul Linux - General 24 02-13-2007 02:54 PM
remove part of string in bash script crewblunts Programming 2 03-16-2006 05:54 PM
Simple bash script help, grabbing part of a string colabus Linux - Newbie 3 04-25-2005 09:42 AM
extracting more than one value from a string ganninu Programming 16 12-10-2003 03:26 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:45 PM.

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