user2740992
user2740992

Reputation:

Sed/grep only the last section of numbers (see example)

I would like to get from this list with grep/sed:

some-text_of-1.6.6-11
text_r6.5.5-text1.6.6-11
text_r6.5.5-something6.6.6-11
text_r6.5.5-yess-2.6.6-11

Only the last numbers, so:

1.6.6-11
6.6.6-11
2.6.6-11

There could be anything(any character) before 1.6.6-11, 2.6.6-11, 6.6.6-11, but nothing after.

Thanks for your answers.

Upvotes: 1

Views: 63

Answers (5)

Jotne
Jotne

Reputation: 41456

Here is one awk

awk -F"[-.a-z]" '{print $(NF-3)"."$(NF-2)"."$(NF-1)"-"$NF}' file
1.6.6-11
1.6.6-11
6.6.6-11
2.6.6-11

By setting Field Separator to -, . or a-z we get the last 4 fields.

Upvotes: 0

nlu
nlu

Reputation: 1963

One possibility would be grep or egrep, limiting the output to the match with -o:

egrep -o "[0-9]\.[0-9]\.[0-9]-[0-9]+" inputfile

Or with sed:

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

Upvotes: 0

NeronLeVelu
NeronLeVelu

Reputation: 10039

sed 'H;s/.*//;x
:cycle
   s/\(.*\)\([-0-9.]\{1,\}\)$/\2\1/
   t cycle
s/\n.*//;s/^[-.]*//' YourFile

more generic to any length of number and dot, minus

Upvotes: 0

Tim Pietzcker
Tim Pietzcker

Reputation: 336158

grep -o [0-9][0-9.-]*$ file

Explanation:

[0-9]    # Match a digit
[0-9.-]* # Match digits, dots or dashes
$        # Anchor the search to the end of the string

The -o option tells grep to only output the matching part of the line instead of the entire line.

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174706

Use sed and sort.

$ sed 's/.*[^0-9]\([0-9]\+\.[0-9]\+\.[0-9]\+-[0-9]\+\)$/\1/g' file | sort -u
1.6.6-11
2.6.6-11
6.6.6-11

Upvotes: 1

Related Questions