kuruvi
kuruvi

Reputation: 653

how to get only version number of a program? pipe into grep

rsync --version

gives lots of info but I just want to grab the first line

rsync  version 3.1.1

How to do this? I tried to pipe into grep but I can't

Upvotes: 3

Views: 1144

Answers (2)

Mark Reed
Mark Reed

Reputation: 95375

There are lots of ways to slice this pie. If you want the whole first line, you can use any of these:

rsync --version | head -n 1
rsync --version | awk NR==1
rsync --version | sed -n 1p
rsync --version | grep '^rsync *version'

If you want just the version number without the rest of the line, that's not much harder, but it depends which part of the line you want. On my Mac, the version line reads rsync version 2.6.9 protocol version 29, and a naïve grab would likely yield the 29 - presumably not what you want. Either of the following will output just the 2.6.9 by itself:

rsync --version | awk 'NR==1 {print $3}'
rsync --version | sed -n '1s/^rsync *version \([0-9.]*\).*$/\1/p'

Upvotes: 4

Steephen
Steephen

Reputation: 15844

If you just need to get the first line of output use head command

 rsync --version | head -1

Upvotes: 2

Related Questions