Linux (49)

Sort on nth field

To sort a text file on a particular field: [code] Sort -t: -k 4n thefile [/code] So thefile will be output, sorted by the 4th field.

watch mysql queries as they happen

cunning bit of Perl, not to be used permanently, but very handy indeed for a quick look at what’s actually being queried… [code] tcpdump -i eth0 -s 0 -l -w – dst port 3306 | strings | perl -e ‘ while(<>) { chomp; next if /^[^ ]+[ ]*$/; if(/^(SELECT|UPDATE|DELETE|INSERT|SET|COMMIT|ROLLBACK|CREATE|DROP|ALTER)/i) { if (defined $q) { print […]

remove blank lines in vi

  • September 1, 2012
  • vi

to remove blank lines in vi: [code]:v/\S/d [/code]

Checking variables

  • August 30, 2012
  • Linux

test if a variable is empty [code][ "$var" ] || echo ‘it be empty'[/code] or check if $var is not null using -n: [code] if [ -n "$var" ]; then # its not null fi [/code] or check if $var is not empty using -ne: [code] if [ -ne "$var" ]; then # its not […]

Changing ownership of files to another user

  • August 30, 2012
  • Linux

Change the ownership of multiple files and directories to a different user: [code]$ find /the/path/ -user olduser -group olduser -exec chown newuser:newgroup {} \;[/code]

Must be root

  • August 30, 2012
  • Linux

A check to see if the user is root. Useful for scripts that must be run as root. [code] # check for root if [ $(id -u) != "0" ]; then echo "no" exit 0 fi echo "ok…" # start the script… [/code]

Recover .swp file in vi

  • August 29, 2012
  • vi

I was just editing a file in vi on my iPad using diet coda. Hadn’t saved it and the app crashed. Boo. Tried to open the .swp file, oops, don’t do that. Instead, use: [code]vi -r file[/code] Open the file as if it existed, but using the -r flag (recover, suprisingly) vi will look for […]

count a unique field in text file

i was just perusing my access_log, and fancied counting the number of times unique ip addresses had access my site. just because. i started by using cat to see the access_log cut the first field using cut -f1 -d’ ‘ sorted that list of ip’s uniq’d it and used -c to add a count then […]

line numbers in vi and goto line in vi

  • August 28, 2012
  • vi

There are some by handy shortcuts in vi, to say the least, it’s a very comprehensive program. So here is how to turn on line numbers in vi and how to goto line in vi. To turn line numbers on, do [code]:set number[/code] To jump to a specific line, a colon followed by the number […]