grep top two results of each subversion in a file in linux

2144 views linux
3

I had a file with subversions i want to grep only top two result from each version.

hostname:~ sagar$ cat test.txt
v9.0
v9.1
v9.2
v9.3
v8.0
v8.1
v8.2
v8.3
v7.1
v7.2
v7.3
v7.4

Desired output:

v9.3
v9.2
v8.3
v8.2
v7.4
v7.3

I have tried sort,grep,head combinations but unable to extract the exact format of top two results set from each version. I want to do this in single line command apart from using script

answered question

1 Answer

11

How about something like this:

cat test.txt | \
  perl -ne '/v(.*)\.(.*)/; $major=$1,$cnt=0 if ($major != $1); print,$cnt++ if ($cnt < 2);'

posted this

Have an answer?

JD

Please login first before posting an answer.