More grep Tips
In addition to grep -v grep, consider the following Unix shell script improvements when using grep(1), to impress your coworkers or babes:
- Count matching lines: grep foo | wc -l. Replace with: grep -c foo.
- Find something, then apply awk(1): grep foo | awk '{print $1}'. Omit the useless grep via: awk '/foo/{print $1}/'.
- Calculate disk usage: du -k . | grep -v '/.*/' | sort -n. Old shell alias I keep around. Now mostly use my diskusage Perl script, which collects and templates the output from du(1). But I digress.
- Fun with pipes and remote systems:
ssh -n $hostname crontab -l | grep -v somecronjob | \ ssh $hostname crontab -
Though Configuration Management should be used to edit system configuration, as random shell scripts like this do not handle future systems of the same type, nor account for unavailable nodes. Also, guess what happens when the following script runs multiple times against the same system(s)?
( ssh -n $hostname crontab -l cat <<'EOF' # cleanup logfiles poorly @daily find /var/log/foodir -mtime +30 -print0 | xargs -0 rm EOF ) | ssh $hostname crontab -
Now I'm really off topic.
$ exunt stage left
Technorati Tags: Unix