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?

2
  • if what you need is the latest revision, see stackoverflow.com/a/111173/393701 Commented Dec 19, 2012 at 15:11
  • @SirDarius No, I don't want the latest version. I want the version in which this directory was introduced. Commented Dec 19, 2012 at 15:40

2 Answers 2

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'`
Sign up to request clarification or add additional context in comments.

1 Comment

This answers one part of my question. But how do I store the result in a shell variable?
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

3 Comments

This answers one part of my question. But how do I store the result in a shell variable?
@Sudar try var=$(svn ...|awk ...) then echo $var you should see the value was stored.
This works, but I am accepting the other answer since he also gave the tcsh version. Upvoted though ..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.