Being the Critic - it's so much fun... - 2006-11-17 20:04:49
Yvan talked about awk. Of course, with all such things, there's always a better way to do things (such as not using awk :)...
Move files to make them uppercase:
ls | awk '{u=toupper($1);if(u!=$1){system("svn move "$1" "u);}}'
Would work better, but still not perfectly as:
ls | awk '{u=toupper($0);if(u!=$0){system("svn move "$0" "u);}}'
That way it handles spaces in filenames - still won't handle newlines in filenames, with a great performance penalty though:
for i in *; do u=`echo "$i" | tr a-z A-Z`; if [ "$i" != "$u" ]; then svn move "$i" "$u"; fi; done
Getting average file size:
find ./corpus/ -type f -exec stat {} \; \
| grep 'Size:' \
| awk '{c+=1;d+=$2}END{print d/c/1024 " kB"}'
There's a useless use of grep, since awk does regexes just fine itself:
find ./corpus/ -type f -exec stat {} \; \
| awk '/Size:/ {c+=1;d+=$2}END{print d/c/1024 " kB"}'
Do some counting with grep:
grep -rihc 'foo' ./corpus/ | awk '{c+=$1}END{print c}'
I can't find anything to pick on in that one. Though doesn't everyone have tiny shell scripts in their bin directory or shell functions/aliases like: add (curse you ancient sum command), avg, min, max?
echo "The rabbit-hole went straight on like a \
tunnel for some way, and then dipped suddenly \
down, so suddenly that Alice had not a moment \
to think about stopping herself before she found \
herself falling down a very deep well." \
| awk -v width=50 -v pre='* ' '
BEGIN{
line=pre;
}
{
for (i = 1; i <= NF; i++)
{
if (length(line)+length($i) > width)
{
print line;
line=pre;
}
line=line$i" "
}
}
END{
print line;
}'
Personally I'd just use :
echo "insert all that text" | fmt -48 | sed 's/^/* /'
Though I must admit fmt gets it wrong on the second line in that example - I have no idea why. It also doesn't compress whitespace (there is -u, but does double spaces after sentences because it's from the 1800s or something).
And as for the first footnote, you should recognise all three! There's no way you've ever come close to UNIX without seeing Peter Weinberger's face. It stares back at you whenever you used the third best text editor amongst many other occurances.