Why are so many people invoking grep
in the pipeline and either using bash
's for
loops or xargs
on the back end when they have bloody awk
right in the middle of things?
First let's get rid of the whimsical use of grep:
svn st | awk '/^?/ { print $2 }'
Since awk
allows you to filter lines based on regular expressions, the use of grep is entirely unnecessary. The regular expressions of awk
aren't that different from grep
(depending on which implementation of awk
and which of grep
you're using) so why add a whole new process and a whole new bottleneck in the pipeline?
From there you already have two choices that would be shorter and more readable:
# option 1
svn st | awk '/^?/ { print $2 }' | xargs rm -f
# option 2
rm -f $(svn st | awk '/^?/ { print $2 }')
Now that second option will only work if your file list doesn't exceed the maximum command line size, so I recommend the xargs version.
Or, perhaps even better, use awk
again.
svn st | awk '/^?/ { system("rm -f $2") }'
This will be the functional equivalent of what you did above with the for
loop. It's grotesquely inefficient, seeing as it will execute rm once per file, but it's at least more readable than your for
loop example. It can be improved still farther, however. I won't go into full details here, but will instead give you comments as clues as to what the final solution would look like.
svn st | awk 'BEGIN{ /*set up an array*/ }; /^?/ { /*add $2 to the array*/ }; END{ /*system("rm -f ...") safe chunks of the array*/}
OK, so that last one is a bit of a mouthful and too much to type off as a routine one-liner. Since you have to "often" do this, however, it shouldn't be too bad to put this into a script:
#!/usr/bin/env awk
BEGIN {
/* set up your accumulator array */
}
/^?/ {
/* add $2 to the array */
}
END {
/* invoke system("rm -f") on safe chunks of the accumulator array */
}
Now your command line will be svn st | myawkscript
.
Now I'll warn you I'm not equipped to check all this (since I avoid SVN and CVS like I avoid MS-DOS -- and for much the same reason). You might have to monkey with the #!
line in the script, for example, or with the regular expression you use to filter, but the general principle remains about the same. And me personally? I'd use svn st | awk '/^?/ { print $2 }' | xargs rm -f
for something I do infrequently. I'd only do the full script for something I do several times a week or more.