Friday, August 27, 2010

Create Dictionary from a nested List

def crdic(a):
b={}
printOP=0
for i in a:
if len(i) != 2:
print 'ERROR - Inner list has more/less than two elements'
printOP=1
break
else:
b[i[0]]=i[1]
if printOP != 1:
print 'Got list : ', a
print 'Created dictionary : ', b

a=[['name','ruchi'],['nickname','r007']]
a1=[['name','ruchi','penguin'],['nickname','r007']]

crdic(a)
crdic(a1)



>>> 
Got list : [['name', 'ruchi'], ['nickname', 'r007']]
Created dictionary : {'nickname': 'r007', 'name': 'ruchi'}
ERROR - Inner list has more/less than two elements
>>>

Thursday, August 26, 2010

SERIES : awk#2 Simple Patterns

cat monkey
Monkey Mink
100 Tree Blvd
Banana County
Monkeyville, MY
Zip 12001
555133 Area 255

Get lines that have a digit anywhere.

awk '/[0-9]+/ {print "Has a digit. : ", $0}' monkey
Has a digit. : 100 Tree Blvd
Has a digit. : Zip 12001
Has a digit. : 555133 Area 255

Get lines that begin with a digit

awk '/^[0-9]+/ {print "Begins with digit. : ", $0}' monkey
Begins with digit. : 100 Tree Blvd
Begins with digit. : 555133 Area 255

Has characters somewhere

awk '/[aA-zZ]+/ {print "Has a word. : ", $0}' monkey
Has a word. : Monkey Mink
Has a word. : 100 Tree Blvd
Has a word. : Banana County
Has a word. : Monkeyville, MY
Has a word. : Zip 12001
Has a word. : 555133 Area 255

Has only letters

awk '/^[aA-zZ]+$/ {print "Has only letters. : ", $0}' monkey
<-- no output -->

Because space is not counted as letters.

Monday, August 23, 2010

SERIES : awk#1 Begin and End

Awk comes with inbuilt loop. It performs the given operations for each line in the input file provided they are not qualified by "BEGIN" or "END".
cat notxt
<-- empty file -->

cat sometxt
monkey goes to market

awk '{print "Hello World!"}' notxt
<--no ouput-->

awk 'BEGIN {print "Hello World!"} {print} END {print "Good bye!"}' notxt
Hello World!
Good bye!

awk 'BEGIN {print "Hello World!"} {print} END {print "Good bye!"}' sometxt
Hello World!
monkey goes to market
Good bye!

ex#1.
Since notxt is empty; awk doesn't iterate and no output is printed.

ex#2.
although notxt is empty; BEGIN and END statements are still executed and output produced for those commands.

Thursday, August 19, 2010

Macro to help remove duplicate rows in excel spreadsheet

Sub duplicate_flg()
' Use to remove duplicates
' Check for duplicates based on columns colx and coly. Customize below.
' flag them in column colflg
' Rowcounts based on column - colx
' ******************************************************
' NEEDs a sorted sheet and assumes a header
' Runs on the active sheet in the active workbook
' ******************************************************

colx = 1
coly = 2
colflg = 3
Cells(1, colflg) = "Is Duplicate?"

lastrowcnt = Cells(Cells.Rows.Count, colx).End(xlUp).Row
'lastrowcnt = 7
ActiveWorkbook.Activate
Set ws = ActiveWorkbook.ActiveSheet

' header assumed. starts from row 2
For i = 2 To lastrowcnt
If ws.Cells(i, colx) = ws.Cells(i + 1, colx) And _
ws.Cells(i, coly) = ws.Cells(i + 1, coly) Then
ws.Cells(i + 1, colflg) = "Y"
End If
Next i

End Sub

Thursday, August 12, 2010

Paste in vi without annoying auto indent

vi annoys the hell out of me with its auto-indenting during clipboard paste. So thanks to this post I've a solution now.

In command mode "set paste" and after pasting the text, turn it back off by "set nopaste".

what a breath of fresh air ;)


Thursday, July 29, 2010

REPLACE between marks

While using vi, add marks to work with a chunk of lines.

To replace beginning of all lines, between the (inclusive) marks a and b, with ZZZ; we could write

:'a,'bs/^/ZZZ/

Where a has lower line number than b.

AWK - IF OR condition

OR operator : ||
AND operator: &&

awk -F, '{if ($1=="abc" || $1=="abd") print $0}' InputFile

-F,
InputFile is comma-separated (Field separator F is comma)

if ($1=="abc" || $1=="abd") print $0
Select and print entire record ($0) when first field ($1) is either "abc" or "abd"