0

When I execute the following svn command, I get the below output.

$ svn log -r 1:HEAD --limit 1 http://plugins.svn.wordpress.org/bulk-delete
------------------------------------------------------------------------
r91525 | plugin-master | 2009-02-03 10:39:23 +0530 (Tue, 03 Feb 2009) | 1 line

adding bulk-delete by sudar
------------------------------------------------------------------------

I am writing a shell script and I want to parse the output of the above command and get the revision number (r91525 in this case) and store it in a shell variable, so that I can use it in the subsequent commands.

I tried the cut command and was able to get the revision number in the second line, but the other lines were still appearing.

$ svn log -r 1:HEAD --limit 1 http://plugins.svn.wordpress.org/bulk-delete | cut -f1 -d'|'
------------------------------------------------------------------------
r91525 

adding bulk-delete by sudar
------------------------------------------------------------------------

Question: How to retrieve the revision number and store in a shell variable, so that I can use it in the subsequent commands?

Sudar
  • 18,954
  • 30
  • 85
  • 131
  • if what you need is the latest revision, see http://stackoverflow.com/a/111173/393701 – SirDarius Dec 19 '12 at 15:11
  • @SirDarius No, I don't want the latest version. I want the version in which this directory was introduced. – Sudar Dec 19 '12 at 15:40

2 Answers2

2

try this :

svn blahblah |awk 'NR==2{print $1;exit;}'

test:

kent$  echo "------------------------------------------------------------------------
r91525 | plugin-master | 2009-02-03 10:39:23 +0530 (Tue, 03 Feb 2009) | 1 line

adding bulk-delete by sudar
------------------------------------------------------------------------"|awk 'NR==2{print $1;exit;}'
r91525
Kent
  • 189,393
  • 32
  • 233
  • 301
  • This answers one part of my question. But how do I store the result in a shell variable? – Sudar Dec 19 '12 at 15:44
  • @Sudar try `var=$(svn ...|awk ...)` then echo $var you should see the value was stored. – Kent Dec 19 '12 at 15:47
  • This works, but I am accepting the other answer since he also gave the tcsh version. Upvoted though .. – Sudar Dec 19 '12 at 16:01
2

You can use sed for this.

  svn log --limit 1 | sed -n -e 's/^\(r[0-9]\+\).*/\1/p'

this will print out only the revision e.g. r1234

If you need only the number of the revision use

  svn log --limit 1 | sed -n -e 's/^r\([0-9]\+\).*/\1/p'

UPDATE: to store it in a variable use

bash:

REVISION=$(svn log --limit 1 | sed -n -e 's/^r\([0-9]\+\).*/\1/p')

tcsh

  set revision = `svn log --limit 1 | sed -n -e 's/^r\([0-9]\+\).*/\1/p'`
dwalter
  • 7,258
  • 1
  • 32
  • 34
  • This answers one part of my question. But how do I store the result in a shell variable? – Sudar Dec 19 '12 at 15:45