atedja
atedja

Reputation: 1516

Unix command to strip off part of a line

I'm working with command-line SVN. Coming from TortoiseSVN on Windows, I found it very tedious whenever I try to pick one specific file to revert, diff, commit, etc., when the path is very long like this:

src/com/company/stuff/platform/appname/subpackage/subsubpackage/Foo.java

If I want to do a diff on this file, I have to type all this:

svn diff src/com/company/stuff/platform/appname/subpackage/subsubpackage/Foo.java

so, I try to grep that by doing this:

svn st | grep Foo

But it returns:

M       src/com/company/stuff/platform/appname/subpackage/subsubpackage/Foo.java

Now, how do I strip off the "M " part, so I can get just the file path?

Upvotes: 2

Views: 268

Answers (4)

sarnold
sarnold

Reputation: 104070

For this I'd use awk:

svn st | awk '/Foo/ { print $2 ; }'

awk takes scripts with matching instructions and blocks of code to execute on the lines that match. This matches lines with Foo in them and prints the second field. (Using the default FS field-separator, which should split on tabs and spaces.)

You should also look into the filename completion abilities of your shell; most newer shells include the ability to complete partially-typed filenames. For your long example:

src/com/company/stuff/platform/appname/subpackage/subsubpackage/Foo.java

I'd probably type it like this:

src/com/com<tab>/stuff/pla<tab>/appn<tab>/subp<tab>/subsub<tab>/Foo<tab>

You might need to type more or less depending upon what files are in your directories.

Upvotes: 1

gcbenison
gcbenison

Reputation: 11963

If 'svn st' always returns an (unwanted) first column, and then the path, try this:

svn st | grep Foo | awk '{print $2}'

Upvotes: 1

jaypal singh
jaypal singh

Reputation: 77105

svn st | grep Foo | sed 's/^[A-Z]\s*//'

Upvotes: 1

Chris Seymour
Chris Seymour

Reputation: 85785

svn st | grep Foo | sed 's/M *//'

Upvotes: 1

Related Questions