Saturday, June 2, 2012

Sed Back References: Defining Regions

Sed has the ability to define regions in the regular expression definition area. This regions can be then back referenced using the special characters "\1" to "\9". To define a region it has to be surrounded with parentheses.

As an example, lets say you have a nine characters long line that has to be splitted into three three-character regions:


Note I'm passing the "-r" argument to sed, otherwise I would have to escape the parenthesis characters to prevent sed to take them as literals.

Following is a "real life" example. I have a plain text file with email addresses and contact names with this format:
name_1:last_name_1:mail_1@domain.com
name_2:last_name_2:mail_2@domain.com
name_3:last_name_3:mail_3@domain.com
name_4:last_name_4:mail_4@domain.com
This could be difficult to read as the file grows. With just one line using sed defining regions, the output of a cat command can be formatted in a "human readable" format:


Thunar Custom Actions: Resize and Rotate Images with Convert

Here I'm posting two simple bash scripts I use along with Thunar Custom Actions to resize and rotate images. Both of them work for on or more selected files.
Rotated images will be changed in place, resizing images will keep the original file and prepend "resized_to_<percent>" to the resized image, where <percent> indicates the selected reduction percent applied.

resize script:
res.sh
     1  #!/bin/bash
     2  
     3  PERCENT="$1"
     4    shift
     5  while (( "$#" )); do  
     6    convert "$1" -resize "$PERCENT%" -quality 100 "resized_to_${PERCENT}%_$1"
     7    shift
     8  done
     9  
    10  exit 0

rotate script:
rot.sh
     1  #!/bin/bash
     2  
     3  
     4  DIRECTION="$1"
     5  shift
     6  
     7  case "$DIRECTION" in
     8    "r")
     9      while (( "$#" )); do
    10        convert -rotate 90 -quality 100 "$1" "$1" 
    11        shift
    12      done
    13    ;;
    14    "l")
    15      while (( "$#" )); do
    16        convert -rotate -90 -quality 100 "$1" "$1"
    17        shift
    18      done
    19    ;;
    20  esac
    21  
    22  exit 0

Adding Custom Actions

Take a look at this thunar wiki article it describes how to add custom actions to Thunar.
The resize script will need the desired resizing percent as an argument.
The rotate script needs the desired rotate direction (l or r), it always rotates in 90° steps.

Openbox Window Manager: Oblogout

I'm using Oblogout to shutdown, restart, etc. from Openbox. It looks very nice and it's easy to configure and customize.

To configure it you will have to edit the "oblogout.conf" file. In Archlinux this file is placed in the "/etc" directory.

To shutdown and restart the system  i'm using the good old "shutdown" command, "xlock" to lock the screen and "openbox --exit" to log out.

out.sh
     1  [settings]
     2  usehal = false
     3  
     4  [looks]
     5  opacity = 70
     6  bgcolor = black
     7  buttontheme = oxygen
     8  buttons = cancel, logout, restart, shutdown, lock
     9  
    10  [shortcuts]
    11  cancel = Escape
    12  shutdown = S
    13  restart = R
    14  logout = L
    15  lock = K
    16  
    17  [commands]
    18  shutdown = sudo shutdown -h now
    19  restart = sudo shutdown -r now
    20  lock = xlock &
    21  logout = openbox --exit

To be able to restart/shutdown without root privileges I added following lines at the end of the "/etc/sudoers" file.
Remember to edit the "sudoers" file using "visudo"

out.sh
    92  %power ALL= (ALL) NOPASSWD: /sbin/shutdown
    93  %power ALL= (ALL) NOPASSWD: /sbin/reboot
    94  %power ALL= (ALL) NOPASSWD: /sbin/halt

oblogout screenshot: