Archive

Archive for May, 2012

vim – Delete lines that do not match a pattern.

May 29, 2012 2 comments

Problem:
I wanted to move some python code from the interactive console to a python file. I needed to remove the >>> and … as well as the output lines.

>>> l1 = [2,2,3,4,5,2]
>>> l1.count(2)
3
>>> l1.count()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: count() takes exactly one argument (0 given)
>>> l1 = [2,2,3,4,5,2]
>>> if 3 in l1: 
...     print "yes"
... 
yes
>>> if 3 not in l1: 
...     print "no"
... 
>>> if 1 not in l1: 
...     print "no"
... 
no
>>> l1
[2, 2, 3, 4, 5, 2]
>>> 

Solution:

From http://vim.wikia.com

The ex command g is very useful for acting on lines that match a pattern. You can use it with the d command, to delete all lines that contain a particular pattern, or all lines that do not contain a pattern.

What I ended up using was

:g!/^\([>>>]\|[\.\.\.]\)/d

Which means delete lines that do not start with either >>> or … (Delete lines that do not match the pattern).

To produce

>>> l1 = [2,2,3,4,5,2]
>>> l1.count(2)
>>> l1.count()
>>> l1 = [2,2,3,4,5,2]
>>> if 3 in l1:
...     print "yes" 
...     
>>> if 3 not in l1:
...     print "no"
...     
>>> if 1 not in l1:
...     print "no"
... 
>>> l1 
>>> 

The rest was simple.

:%s/\(>>>\|\.\.\.\) //

Final Result

l1 = [2,2,3,4,5,2]
l1.count(2)
l1.count()
l1 = [2,2,3,4,5,2]
if 3 in l1: 
    print "yes"

if 3 not in l1: 
    print "no"

if 1 not in l1: 
    print "no"

l1

Source:
http://vim.wikia.com/wiki/Delete_all_lines_containing_a_pattern

NB:

:g/^\([>>>]\|[\.\.\.]\)/d

Means delete lines that match the pattern. (Lines that start with >>> or …).

Categories: vim Tags:

bash – When cron fails

May 25, 2012 Leave a comment

Problem:
A cron job did not run.

A colleague of mine once said “cron never fails”. From what I have observed he was 99% right. One time a job did not run because the machine was off, the other time it did not run at the expected time because the system date had been reset. All other times cron has run when expected. The scripts to be run by cron though, have failed many times due to various reasons. Yesterday, I had some issues with a script and I am making a note of it here just to document my cron job issues.(Not including network issues)

Solution:
Usually when this happens you check the following.

1. Did the script run in the correct location?
Make sure to always change to the correct directory at the start of your script.

2. Does the script have execute permissions?
chmod +x script.sh

3. The script itself has errors. So log them.
./script.sh 2 > script.log. (ie redirect standard output to a log file.)

Categories: bash Tags: ,

bash – change shell colour output

May 24, 2012 2 comments

Problem:
I wanted to have different colours for different output.

Solution:
Lets start with some examples.
Eg 1 – from chihungchan.blogspot.com

$red="\033[31m"
$green="\033[32m"
$off="\033[0m"
$echo -e "${red}Error. Unable to run command${off}"
$echo -e "${green}OK. Run successful.${off}"

Eg 2 – from unix.com

LINE=$(grep success data.log)

if [ $? -eq 0 ]
then
        printf "\033[01;32m%s\033[00m\n" "$LINE"
else
        printf "\033[01;31m%s\033[00m\n" "'success' not found in log"
fi

Explaination.
From chihungchan.blogspot.com

  • Text properties – 0(default), 1(bold), 22(not bold), 4(underlined), 24(not underlined), 5(blinking), 25(not blinking), 7(invers), 27(not invers)
  • Foreground colour – 30(black), 31(red), 32(green), 33(yellow), 34(blue), 35(magenta), 36(cyan), 37(white)
  • Background colour – 40(black), 41(red), 42(green), 43(yellow), 44(blue), 45(magenta), 46(cyan), 47(white)

So in this example
# Text property 1 (bold)
# Foreground Colour 31 (red)
# Background Colour 42 (green)
# The command below gives me Red Text in bold with a green background.

$somecolor="\033[1;31;42m" 
$echo -e "${somecolor}OK. Run successful.${off}"

shell colours

Source:
1. http://chihungchan.blogspot.com/2008/10/give-your-shell-some-colours.html
2. www.unix.com

Update (2012-06-05): Found more information on bash colors.
http://zipizap.wordpress.com/2011/09/28/quick-bash-colors/
http://zipizap.wordpress.com/2012/04/09/quick-bash-colors-2-scripting/

Update (2012-07-17):
ANSI color escape sequences

Categories: bash Tags:

bash – list directories only

May 23, 2012 Leave a comment

1. List directories only
ls -d */

2. Order directory listing by time
ls -dt */

Source:
www.unix.com

Categories: bash Tags:

awk – Get the sum of a column based on a second column

May 15, 2012 Leave a comment

Problem:
I had a list of totals for various days in the month and I needed to add them up.
From

2012-05-09,2
2012-05-10,2
2012-05-11,3
2012-05-12,3
2012-05-13,2
2012-05-14,2
2012-05-01,3
2012-05-02,4
2012-05-03,2
2012-05-04,2
2012-05-10,2
2012-05-11,2
2012-05-12,1
2012-05-13,2
2012-05-14,2

To

2012-05-01,3
2012-05-02,4
2012-05-03,2
2012-05-04,2
2012-05-09,2
2012-05-10,4
2012-05-11,5
2012-05-12,4
2012-05-13,4
2012-05-14,4

Solution
I was looking for an awk solution and found on at unix.com
Awk + sort

user@computer:~$echo "2012-05-09,2                                                                       
2012-05-10,2
2012-05-11,3
2012-05-12,3
2012-05-13,2
2012-05-14,2
2012-05-01,3
2012-05-02,4
2012-05-03,2
2012-05-04,2
2012-05-10,2
2012-05-11,2
2012-05-12,1
2012-05-13,2
2012-05-14,2" | awk -v FS=, -v OFS=, '{a[$1]+=$2}END{for (i in a) print i,a[i]}' | sort -t, -k1

Awk explained:

The first {…} block creates an array a indexed with parameter $1, adding $2.
When ‘2012-05-13’ on the first column is encountered the first time, you end up with a[‘2012-05-13’]=2, then the second time a[‘2012-05-13’]+=2 (a[‘2012-05-13’] is already 2, plus 2 = 4).

The second {…} block takes each index from the a array, store them in i, then displays i and the corresponding a[‘i’]

Source
http://www.unix.com/shell-programming-scripting/126311-print-unique-values-column-sum-up-corresponding-values-next-column.html

Categories: awk, bash

May 4th – Day against DRM

May 4, 2012 Leave a comment
Categories: Interesting Tags: ,