ABI
ABI

Reputation: 25

Pattern extraction using SED or AWK

How do I extract 68 from v1+r0.68?

Upvotes: 0

Views: 753

Answers (5)

Chris Seymour
Chris Seymour

Reputation: 85913

Match the digit string before the end of the line using grep:

$ echo 'v1+r0.68' | grep -Eo '[0-9]+$'
68

Or match any digits after a .

$ echo 'v1+r0.68' | grep -Po '(?<=\.)\d+'
68

Print everything after the . with awk:

echo "v1+r0.68" | awk -F. '{print $NF}'
68

Substitute everything before the . with sed:

echo "v1+r0.68" | sed 's/.*\.//'
68

Upvotes: 1

timidpueo
timidpueo

Reputation: 320

Using awk, returns everything after the last '.'

echo "v1+r0.68" | awk -F. '{print $NF}'

Upvotes: 4

AWE
AWE

Reputation: 4135

type man grep

and you will see

...

   -o, --only-matching

          Show only the part of a matching line that matches PATTERN.

then type echo 'v1+r0.68' | grep -o '68'

if you want it any where special do:

echo 'v1+r0.68' | grep -o '68' > anyWhereSpecial.file_ending

Upvotes: 0

Kent
Kent

Reputation: 195289

grep is good at extracting things:

kent$  echo " v1+r0.68"|grep -oE "[0-9]+$" 
68

Upvotes: 1

perreal
perreal

Reputation: 98118

Using sed to get the number after the last dot:

echo 'v1+r0.68' | sed 's/.*[.]\([0-9][0-9]*\)$/\1/'

Upvotes: 1

Related Questions