Callback Kid
Callback Kid

Reputation: 718

AWK doesn't properly tokenize input as part of git alias

I am trying to add a git alias to prompt me to delete all the local git branches. I have come pretty far an I've arrived at the following:

pdb = "! sh -c 'git branch -vv | grep -v \"origin/.*\" | awk \"{print $1}\" | xargs -L1 -I{} -p git branch -D {}' -"

The first part will list all branches with verbose output so that when piped to grep, we can filter out the branches with a remote counterpart. awk should then take the first part of that output, the branch name, and send it off to the xargs command. The trouble I am having is that awk is passing the entire verbose output to xargs.

This answer got me a little closer but I am still not quite getting the expected output from awk https://stackoverflow.com/a/25954749/3356508

$ git pdb
git branch -D master-test1 9837ec0f0 test commit 1 ?...y
error: branch 'master-test1 9837ec0f0 test commit 1' not found.
git branch -D master-test2 2810823bc test commit 2 ?...y
error: branch 'master-test2 2810823bc test commit 2' not found.

Upvotes: 1

Views: 186

Answers (1)

Poshi
Poshi

Reputation: 5762

Cannot test your issue, but I think it is due the fact that you are not escaping the $ in awk, so you end up doing a simple print, which, by default, prints everything.

Try with:

pdb = "! sh -c 'git branch -vv | grep -v \"origin/.*\" | awk \"{print \\$1}\" | xargs -L1 -I{} -p git branch -D {}' -"

Upvotes: 1

Related Questions