LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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
  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: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:27 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
Open Source Consulting | Domain Registration