1

If I get a list of files that have changed how can I pull a certain string out and print it?

git diff --name-only origin/master

Output:

server/packages/sharing/src/test1.js
client/packages/sharing/src/test2.js
client/packages/foo/src/test3.js
client/packages/foo/src/test4.js
client/packages/bar/src/test5.js
client/packages/bar/src/test6.js
client/packages/baz/src/test7.js
client/packages/baz/src/test8.js

I would like to get the third word in the path when examining the file list.

Trying to get:

sharing, foo, bar, baz

or

sharing
foo
bar
baz

I tried git diff --name-only origin/master | grep -o '*/packages/*/src' but I'm not sure how to capture the word. (using zsh on a Mac if it helps with narrowing down what's on my system)

1 Answer 1

3

You can decompose your path with cut command :

git diff --name-only origin/master | cut --delimiter / --fields 3

On Mac, try :

git diff --name-only origin/master | cut -d/ -f3
2
  • On mac you are bound to use the short argument name -d and -f.
    – Bruno
    Apr 30, 2020 at 21:21
  • git diff --name-only origin/master | cut -d/ -f3 | uniq helps remove dupes Apr 30, 2020 at 21:23

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.