Live data from Hacker News

Ask HN: What are some small scripts you use daily?

news.ycombinator.com

51–59 of 59 posts

Re: Ask HN: What are some small scripts you use daily?

#51
post #26

This is awk which emits the stream of unique things, as they are seen. it doesn't require sorted input. It runs at the cost of building the obvious hash in memory so can drive you to swap over large inputs, but its portable, does not require post-install s/w typically not on small systems and it delivers outcomes fast. I use it all the time when I have some UNIX pipe emitting things and I want to "see" the uniques be…

I do something similar with Perl, since I know the syntax a bit better. It allows me to (from memory) scrub out non-unique things like timestamps.

So, without the scrubbing:

tail -f somefile | perl -ne '!$SEEN{$_}++ and print'

Scrubbing off leading timestamps:

tail -f somefile | perl -ne 's/^[0-9:]//;!$SEEN{$_}++ and print'

Re: Ask HN: What are some small scripts you use daily?

#52
post #26

This is awk which emits the stream of unique things, as they are seen. it doesn't require sorted input. It runs at the cost of building the obvious hash in memory so can drive you to swap over large inputs, but its portable, does not require post-install s/w typically not on small systems and it delivers outcomes fast. I use it all the time when I have some UNIX pipe emitting things and I want to "see" the uniques be…

[deleted]

Re: Ask HN: What are some small scripts you use daily?

#53
My thinkpad running Linux is a bit temperamental when changing displays, often enumerating an existing display port as a new one.

I use the following script to switch to dual external monitors at a standard resolution, and a counterpart script to switch back to the internal hidef monitor.

If only I could reliably fix xfce4's panel placement all of the time...and not have to restart chrome and pycharm/intelliJ on each display change!

  #!/bin/bash
  
  EXT1=`xrandr --current | sed 's/^\(.*\) connected.*$/\1/p;d' | grep -v ^eDP | head -n 1`
  EXT2=`xrandr --current | sed 's/^\(.*\) connected.*$/\1/p;d' | grep HDMI | head -n 1`
  INT=`xrandr --current | sed 's/^\(.*\) connected.*$/\1/p;d' | grep -v ^DP | head -n 1`
  
  
  xfconf-query -c xsettings -p /Xft/DPI -s 96
  xfconf-query -c xfce4-panel -p /panels/panel-1/size -s 28
  
  xrandr \
  --output VIRTUAL1 --off \
  --output ${INT} --off \
  --output ${EXT2} --mode 1680x1050 --pos 0x150 --rotate normal \
  --output ${EXT1} --mode 1920x1200 --pos 1680x0 --rotate normal --primary

Re: Ask HN: What are some small scripts you use daily?

#54
post #10

I have a small perl script I wrote called helpme by default it shows a list of topics. Then if you run it with the topic, it displays the details about the topic. I use it to remember how to do less frequent stuff at my day job.

How does it work?

Its super simple 11 lines of code then lines for the topics and description.

I have a topics hash/dictionary.

If the helpme command is run without a topic argument, it prints out all the topic keys to the dictionary one per line.

If it is run with a topic argument, it just prints out the value for that topic.

Re: Ask HN: What are some small scripts you use daily?

#56

I have a lil thing I call "did" that just greps my shell history for a term, sorting and de-duplicating the result: $ cat `which did` grep $1 ~/.bash_history | sort | uniq I also set HISTSIZE=10000000 I don't mind having a large history file laying around because it's so useful.

Thanks for this one. I hadn't stumbled on a good way to search my history before.

Re: Ask HN: What are some small scripts you use daily?

#57
Something that I added to my bash profile is this simple `cd` override that triggers `workon` from `virtualenvwrapper` if the folder I'm accessing has a `virtualenv` with the same name.

    cd()
    {
      command cd "$@"
      [[ "$OLDPWD" == "$HOME"/Work/* && ! -z "$VIRTUAL_ENV" && "$OLDPWD" == *"${VIRTUAL_ENV##*/}"* && "$PWD" != *"${VIRTUAL_ENV##*/}"* ]] && deactivate
      [[ -z "$VIRTUAL_ENV" && "$PWD" == "$HOME"/Work/* ]] && next="$(sed -e "s/^.*Work\///" -e "s/\/.*$//" 
Yes, too many one-liners and short-circuits are usually a no-no, but it's not like anyone else is ever going to use this and I like these shortcuts.

Re: Ask HN: What are some small scripts you use daily?

#58
post #56

I have a lil thing I call "did" that just greps my shell history for a term, sorting and de-duplicating the result: $ cat `which did` grep $1 ~/.bash_history | sort | uniq I also set HISTSIZE=10000000 I don't mind having a large history file laying around because it's so useful.

Thanks for this one. I hadn't stumbled on a good way to search my history before.

Cheers. :)

Re: Ask HN: What are some small scripts you use daily?

#59
post #38
post #13

I created a Perl script called anyconnector which allows me to jump around between different Cisco Anyconnect VPN's using details stored in KeePass entries. e.g. To connect: anyconnector -c env-name Disconnect: anyconnector -d Get status: anyconnector -s It gets used by my team all day, every day.

Looks pretty handy! How do you interface with the KeePass database? Is there a library? with Perl bindings?

There's quite a neat little library with an impressive amount of functionality for interacting with Keepass, called "File::KeePass;".

Here's a copy of the script itself: https://gist.github.com/kjbweb/38508fd92669101ec1fca4bea62bc...

It works on Mac's too, so I've been told.

Post reply on HN