Thursday, October 28, 2010

When ssh relentlessly keeps asking for password

Check if your from and to home directories plus ssh directory have the correct permissions. All the details here -

Using python shell for django project

If you want to test django code in python shell but are getting error messages regarding environment variables not set; set them by using that project's manage script.
python manage.py shell

Thursday, September 9, 2010

list directories

Listing directories in UNIX korn shell
$ls -d */
Mail/ sourcefiles/ mib/ dir3/
awc/ locks/ targetfiles/ dir4/
bin/ log/ perlo/ z12/

Monday, August 30, 2010

SERIES : awk#4 Simple Calculations

This is the same employee file that we used in the previous post.
$cat empf
100,Smith James,80000,101 Ave N,San Francisco,CA
200,Lloyd Beth,80000,2010 University St,San Francisco,CA
300,Doe John,90000,1005 Royal Dr,Hartford,CT
400,Day Matt,85000,100 W 4th Ave,Seattle,WA
700,Bell Amy,60000,201 Winner CT,Atlanta,GA
800,Hawn Ruby,75000,120 Red Carpet Ln,Los Angeles,CA
While creating the script, it's required to keep "{" on the same line as BEGIN and END. The script errors out otherwise.
NR is the inbuilt awk variable representing "Number of Records"
#!/usr/bin/awk
BEGIN{
FS=","
}
{
# sal $3 , name is $2
saltotal=saltotal+$3
}
END{
print "Number of Employees :", NR
print "Salary Total :", saltotal
print "Average Salary : ", saltotal/NR
}
Here's the output.
$awk -f avgsal.awk empf
Number of Employees : 6
Salary Total : 470000
Average Salary : 78333.3
Optionally, we can pass the file separator on the command line instead of having a BEGIN block in the script.

Note how awk automatically provides a float data type.

SERIES : awk#3 Match only a specific field

Here's the input file with employee data.

$cat empf
100,Smith James,80000,101 Ave N,San Francisco,CA
200,Lloyd Beth,80000,2010 University St,San Francisco,CA
300,Doe John,90000,1005 Royal Dr,Hartford,CT
400,Day Matt,85000,100 W 4th Ave,Seattle,WA
700,Bell Amy,60000,201 Winner CT,Atlanta,GA
800,Hawn Ruby,75000,120 Red Carpet Ln,Los Angeles,CA
Let's say we want only Connecticut CT employees. We could look for "CT" in the file.

$awk '/CT/ {print $0}' empf
300,Doe John,90000,1005 Royal Dr,Hartford,CT
700,Bell Amy,60000,201 Winner CT,Atlanta,GA
To check for a pattern strictly against a field, we can use tilde operator "~"

$awk -F, '$6 ~ /CT/ {print $0}' empf
300,Doe John,90000,1005 Royal Dr,Hartford,CT
On a similar note, to exclude all the CA employees.

awk -F, '$6 !~ /CA/ {print $0}' empf
300,Doe John,90000,1005 Royal Dr,Hartford,CT
400,Day Matt,85000,100 W 4th Ave,Seattle,WA
700,Bell Amy,60000,201 Winner CT,Atlanta,GA

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.