42 ms·
Quick hack - you can include the commands you pipe to with this: history \ | sed "s/^[0-9 ]*//" \ | sed "s/ *| */\n/g \
by ColinWright 14y ago
Quick hack - you can include the commands you pipe to with this:
history \
| sed "s/^[0-9 ]*//" \
| sed "s/ *| */\n/g \
| awk '{print $1}' \
| sort \
| uniq -c \
| sort -rn \
| head -n 100 \
> commands.txt
I haven't tried to account for pipe symbols inside strings - it didn't seem work it.
In case there are commands you want then to exclude (which I do) then you might want to "head -200", remove the commands you don't want to provide, and then trim to 100.
Added in edit - having done this a good half of my top 100 are actually scripts, so this is pretty pointless for me unless I rummage through them to find the common commands.
Added in edit again:
OK, here's a version that only includes actual system commands, and hence filters out all my personal scripts and commands:
history \
| sed "s/^[0-9 ]*//" \
| sed "s/ *| */\n/g" \
| awk '{print $1}' \
| xargs which \
| sed "s.^/usr.." \
| grep ^.bin \
| sed "s/^.*\///" \
| sort \
| uniq -c \
| sort -rn \
> commands.txt
- hackerpants 14y agoThanks very much! As of 2:14pm, it's using your new version for greater pipe accuracy =)
- __david__ 14y agoI think you want s///g on your second sed... Also that doesn't work in Mac OS X for some reason (their sed doesn't appear to interpret \n in the replacement text). I replaced it with perl to make that part work: perl -pe 's/ *\| */\n/g' I still haven't gotten the whole thing to work yet because my history contains the above history pipeline and so it's splitting the "|" that inside the sed command onto multiple lines which is causing "xargs which" to balk because quotes are not matching or something: xargs: unterminated quote Shells are amazing until spaces or quotes are involved! :-)
- ColinWright 14y agoYes, I've inserted the "g" in the appropriate sed commands. Thanks - good catch. Some systems seem to require \r instead of \n - I know vim's behavior differs from sed's in this, so that might be an issue. With xargs balking, you can throw the error stream at that point, for convert it to a loop over the alleged commands: for c in $( long thing before the xargs ) do which $c done \ | long thing after the xargs. Specifically: for f in $( \ history \ | sed "s/^[0-9 ]*//" \ | sed "s/ *| */\n/g" \ | awk '{print $1}' \ ) do which $f 2> /dev/null done \ | sed "s.^/usr.." \ | grep ^.bin \ | sed "s/^.*\///" \ | sort \ | uniq -c \ | sort -rn \ > commands.txt And yes spaces are a pig, and can lead to all sorts of ambiguities that don't have reasonable ways of resolving them, especially in filenames.
- __david__ 14y ago"\r" doesn't work either. It's just not interpreting those kind of escapes. I get lines like this: historyrsed "s/^[0-9 ]*//"rsed 's/ *r*/\r/g'rless The for loop is a good idea, though I prefer the "while read" idiom since it fits in with the pipeline better: ... | awk '{print $1}' \ | while read line; do which $line; done \ ... That finally works for me. Here's the whole thing: history \ | sed "s/^[0-9 ]*//" \ | perl -pe 's/ *\| */\n/g' \ | awk '{print $1}' \ | while read line; do which $line; done \ | sed "s.^/usr.." \ | grep ^.bin \ | sed "s/^.*\///" \ | sort \ | uniq -c \ | sort -rn
- lobotryas 14y agoFrom what I recall to represent s newline in sed on osx you need to add an actual carriage return. Take the poster's script above and copy it to a text file. Now replace \n with a new line, save and it should run.