3 ms·
Awk is a command I turn to time and again. For me it's the single most valuable command for enabling the piped single-purpose pattern. As an example, if I want
by Communitivity 7y ago
Awk is a command I turn to time and again. For me it's the single most valuable command for enabling the piped single-purpose pattern.
As an example, if I want a sorted list of all open files under the home directories on CentOs I can do this:
lsof | awk '{ print $10 }' | grep ^/home/ | sort | uniq
- pge 7y agoyou can save one command by using sort -u which eliminates dupes in the sort, removing the need for uniq
- kbr2000 7y agoTnx, that one I didn't know... You can drop another command as such: lsof |awk '($10 ~ /^\/home\//) {print $10}' |sort -u
- kmstout 7y agoDon' stop there. You've solved a real problem in your life, and you might want that information another day. Make a tiny script that encapsulates it. Generalize it a tiny bit, and give it a memorable name (perhaps lsof-tree). That done, you can stop worrying about the mechanics of the solution and build on it. #! /bin/bash # lsof-tree: list open files in a given directory tree (default /home) NAME=9 # set to 10 for CentOS BASE="${1:-/home}" lsof | awk -v NAME=$NAME '{print $NAME}' | grep "^$BASE" | sort -u
- jabl 7y agoYMMV, but I find it easier and faster to know the basic utilities and how to compose them with pipes than remembering the name of a zillion such scripts. I guess it depends on how often you need that particular pipeline. Every day? Sure, make a script. Every few months? Nah, I won't remember it anyway,or probably I remember that I've made a script like that but then I have to start searching my bin directory in the end using more time than just writing the pipeline in the first place.
- Communitivity 7y agoSpot on. My ~/bin has a large number of such little scripts, and my current project has a 'scripts' repository for the same reason.