Source: http://unix.stackexchange.com/questions/84922/extract-a-part-of-one-line-from-a-file-with-sed

Imagine you have a text:

# cat weird.txt
Number of ducks 120 how many of them are singers 100
15 humans in the choir class, and 12 can sing
Fat turles: 300, How many can sing: 100, How many can dance: 100

How do we extract the first number (number of animal) and the second number (number of singers). Notice that there is no pattern. so we have to tell sed literally get us the first set of numbers you encounter and the second.

So to extract the first number you would do this:

cat weird.txt | sed -r 's/([^0-9]*([0-9]*)){1}.*/\2/'

The second numbers:

cat weird.txt | sed -r 's/([^0-9]*([0-9]*)){2}.*/\2/'

The third set of numbers (which only appears once on the third line):

cat weird.txt | sed -r 's/([^0-9]*([0-9]*)){3}.*/\2/'

Output of all 3 runs:

# extract 1st numbers
# cat w.txt |  sed -r 's/([^0-9]*([0-9]*)){1}.*/\2/'
120
15
300

# extract 2nd numbers
# cat w.txt |  sed -r 's/([^0-9]*([0-9]*)){2}.*/\2/'
100
12
100

# extract 3rd numbers (notice the 2 blank lines, because those lines didnt have a 3rd line)
# cat w.txt |  sed -r 's/([^0-9]*([0-9]*)){3}.*/\2/'


100

# TIP: to remove those extra blank lines use "grep ." (which skips blank lines, unless they have spaces or tabs)
# cat w.txt |  sed -r 's/([^0-9]*([0-9]*)){3}.*/\2/' | grep .

The end.

Leave a Reply

Your email address will not be published. Required fields are marked *