4 ms·
Interesting, I assume you do development? How do you handle scripting, moving files, grepping logs etc?
by Gravityloss 10y ago
Interesting, I assume you do development? How do you handle scripting, moving files, grepping logs etc?
- pjmlp 10y agoGUI tooling? Languages like Python?
- umanwizard 10y agoHow would you do this with GUI tooling: Find all files containing a particular string in a directory tree. For each of those files, if its filename appears in a file called Whitelist, write it to a file called Output. Otherwise, do nothing. Here is one possible command line version (I haven't tested it as I'm on mobile): TMPF=`mktemp /tmp/XXXXXXXX` && ag MyString | awk -F: '{print $1}' | sort | uniq > $TMPF && comm -12 Whitelist $TMPF > Output && rm $TMPF
- hawski 10y agoYou probably could use process substitution to get rid of the temp file: comm -12 Whitelist <(ag MyString | awk -F: '{print $1}' | sort | uniq) > Output Then you could use unique flag of sort: comm -12 Whitelist <(ag MyString | awk -F: '{print $1}' | sort -u) > Output Also ag can list only matching files (getting rid of awk and unique): comm -12 Whitelist <(ag -l MyString | sort) > Output
- umanwizard 10y agoThanks for the tips!
- belltaco 10y agoWould be trivial to write a GUI program to do the same.
- umanwizard 10y agoI think it would take at least a half hour, unless you have directory traversal, file I/O, and regex APIs memorized which most people don't. That's "trivial" in the grand scheme of things but it's really not practical to do day-in and day-out every time some random scripting necessity arises. And you presumably have to clutter up your filesystem with random project files, etc, which is annoying. Are you going to have 1000 of these, one for every time you need to do some sort of minor task like this? That command, on the other hand, took me 2 or 3 minutes to write, and I had to look up the syntax for `mktemp`. If I had remembered the mktemp syntax it would have taken a few seconds.
- pjmlp 10y agoApparently you forgot to read the part where I mentioned "Languages like Python?". import glob import shutil whitelist = set(open('Whitelist').read().split()) for fd in [ x for x in glob.glob('mydir/*', recursive=True) if x in whitelist]: shutil.copy(fd, 'OUTPUT') Better, no need to create temporary files.