LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   extracting part of a string (sed, bash, ...) (https://www.linuxquestions.org/questions/programming-9/extracting-part-of-a-string-sed-bash-612840/)

rama76 01-11-2008 07:49 AM

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.

druuna 01-11-2008 07:58 AM

Hi,

Using sed:

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

or

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

Hope this helps.

rama76 01-11-2008 08:05 AM

This helps indeed.
Thank you druuna.

pixellany 01-11-2008 08:14 AM

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


All times are GMT -5. The time now is 03:09 PM.