awk/sed print all the digits before matching symbol

0

I have input with lines like this (it's a CIGAR string from sam format but it doesn't matter now):

123M76N55M4S
6M7N25M
32M488N

And I want to extract all the N's and preceding digits:

76N
7N
488N

I've tried this command:

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

But I get in the output only the last digit. How can I obtain all the preceding digits?

Poiu Rewq

Posted 2017-02-08T13:42:37.413

Reputation: 3

Answers

1

This will suffice:

grep -o '[0-9]\+N'

Your sed regex fails because the .* is too greedy. You have to make sure you don't consume any of the digits:

sed -r 's/(.*[^0-9])?([0-9]+N).*/\2/'

glenn jackman

Posted 2017-02-08T13:42:37.413

Reputation: 18 546