LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
LinkBack Search this Thread
Old 01-11-2008, 07:49 AM   #1
rama76
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
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

Using sed:

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

or

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

Hope this helps.
 
Old 01-11-2008, 08:05 AM   #3
rama76
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
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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)

Code:
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
 
  


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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are 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


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration