Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract numbers from a string using sed and regular expressions

Tags:

regex

integer

sed

Another question for the sed experts.

I have a string representing an pathname that will have two numbers in it. An example is:

./pentaray_run2/Trace_220560.dat 

I need to extract the second of these numbers - ie 220560

I have (with some help from the forums) been able to extract all the numbers together (ie 2220560) with:

sed "s/[^0-9]//g" 

or extract only the first number with:

sed -r 's|^([^.]+).*$|\1|; s|^[^0-9]*([0-9]+).*$|\1|' 

But what I'm after is the second number!! Any help much appreciated.

PS the number I'm after is always the second number in the string.

like image 898
Steven Avatar asked Oct 19 '12 12:10

Steven


People also ask

How extract only numbers from string in shell script?

Replaces all non numbers with spaces: sed -e 's/[^0-9]/ /g' Remove leading white space: -e 's/^ *//g' Remove trailing white space: -e 's/ *$//g' Squeeze spaces in sequence to 1 space: tr -s ' '

Does sed work with regex?

Although the simple searching and sorting can be performed using sed command, using regex with sed enables advanced level matching in text files. The regex works on the directions of characters used; these characters guide the sed command to perform the directed tasks.

How do you substitute sed?

Find and replace text within a file using sed command Use Stream EDitor (sed) as follows: sed -i 's/old-text/new-text/g' input. txt. The s is the substitute command of sed for find and replace.

What is expression in sed?

5.1 Overview of regular expression in sed A regular expression is a pattern that is matched against a subject string from left to right. Most characters are ordinary: they stand for themselves in a pattern, and match the corresponding characters. Regular expressions in sed are specified between two slashes.

How to extract numbers from a string using regular expressions?

How to extract numbers from a string using regular expressions? You can match numbers in the given string using either of the following regular expressions − Enter sample text: this is a sample 23 text 46 with 11223 numbers in it Digits in the given string are: 23 46 11223

How to find all or a portion of a string using regex?

Regular expression is the regular expression for the string you would like to find, note that it must appear in quotation marks. For regexs, that is, to recall all or a portion of a string, the syntax is: Where n is the number assigned to the substring you want to extract. The substrings are actually divided when you run regexm.

What is a regular expression?

"A regular expression (shortened as regex or regexp; also referred to as rational expression) is a sequence of characters that specifies a search pattern." How does a Regular Expression help us pull out phone numbers throughout the long text? For example, you are looking for a way to extract at once all phone numbers from the text.

How to extract all phone numbers using regex tool in octoparse?

Each phone number starts with <li> and ends with </li>. And we can use the RegEx Tool in Octoparse to quickly extract all phone numbers. Step 1. Run Octoparse and open the RegEx Tool. Step 2. Copy and paste the source code in the "Source Text" box. Then select "Start With" option and enter "<li>". Step 3.


1 Answers

is this ok?

sed -r 's/.*_([0-9]*)\..*/\1/g' 

with your example:

kent$   echo "./pentaray_run2/Trace_220560.dat"|sed -r 's/.*_([0-9]*)\..*/\1/g' 220560 
like image 106
Kent Avatar answered Oct 03 '22 15:10

Kent