Monday, October 25, 2010

Taglist in shell

Tag list source code.

Source

Shell word frequency filter

Also available here .

#!/bin/sh
# standard output.
#
# Usage:
# wf [ n]
tr -cs A-Za-z\' '\n'|
tr A-Z a-z |
sort |
uniq -c |
sort -k1,1nr -k2 |
sed ${1:-25}q
e.g.
cat fileName.txt | ./wf.sh 12

Description :

tr -cs A-Za-z\' ' \n' | Replace nonletters with newlines
  tr A-Z a-z | Map uppercase to lowercase
    sort | Sort the words in ascending order
      uniq -c | Eliminate duplicates, showing their counts
        sort -k1,1nr -k2 | Sort by descending count, and then by ascending word
          sed ${1:-25}q Print only the first n (default: 25) lines;


$ wf 999999 < filename.txt | awk ' $1 >= 5' | wc -l
number of unique words occurring at least 5 times

$ wf 999999 < hamlet | tail -n 12 | pr -c4 -t -w80
some of the least frequent words

$ wf 999999 < hamlet | wc -l
number of unique words

$ wf 999999 < hamlet | grep -c ' ^ *1•'
or

$ wf 999999 < hamlet | egrep -c '[[:space]]+1[[:space:]]+'

number of words used exactly once



Source

Friday, October 22, 2010

tr shell command example

cat file(s) | tr A-Z a-z | tr -c a-z\' ' \n' 

The second pipeline stage converts uppercase to
 lowercase, the third replaces nonletters by newlines.
The third stage treats apostrophes as letters.

shell script for converting tab separated values to html table

source

#! /bin/sh
# Convert a tab-separated value file to grammar-conformant HTML.
#
# Usage:
#    tsv-to-html < infile > outfile
cat << EOFILE Leading boilerplate
<! DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN//3. 0">
<HTML>
    <HEAD>
        <TITLE>
            Office directory
        </TITLE>
        <LINK REV="made" HREF="mailto: $USER@` hostname` ">
    </HEAD>
    <BODY>
        <TABLE>
EOFILE
sed -e ' s=&=\&amp; =g' \ Convert special characters to entities
    -e ' s=<=\&lt; =g' \
    -e ' s=>=\&gt; =g' \
    -e ' s=\t=</TD><TD>=g' \ And supply table markup
    -e ' s=^. *$=            <TR><TD>&</TD></TR>='
cat << EOFILE Trailing boilerplate
        </TABLE>
    </BODY>
</HTML>
EOFILE

a fantastic resource for unix command line examples

http://publib.boulder.ibm.com/infocenter/aix/v6r1/index.jsp?topic=/com.ibm.aix.cmds/doc/aixcmds3/join.htm

cut and awk for same job : extract fields and join them

$ cat test.txt
a:b:c:d:e
a1:b1:c1:d1:e1

Welcome@computer_1012 ~
$ cat test.txt | cut -d: -f1,5 --output-delimiter="-"
a-e
a1-e1

Welcome@computer_1012 ~
$ cat test.txt | awk -F: '{print $1 "-" $5}'
a-e
a1-e1

shell pattern matching

The problem is that the asterisks can match anything,
 even the commas used as delimiters. However,
 if you add the delimiters to the pattern, you can
 no longer match the ends of the list:

list=orange, apple, banana
case $list in
*, orange, *)        echo "The only fruit for which there is no Cockney slang. "; ;
esac

[no output]

To resolve this, wrap the list in an extra set of delimiters when expanding it:
list=orange, apple, banana
case , $list, in
*, orange, *)        echo "The only fruit for which there is no Cockney slang. "; ;
esac
The only fruit for which there is no Cockney slang.

shell case switch


#! /bin/sh
pattern="$1"
shift
echo " Matching against ' $pattern' : "
for string
do
  case $string in
  $pattern) echo " $string: Match. " ; ;
  *) echo "$string: No match. " ; ;
  esac
done

Save this script to a file named pattern, make it executable (chmod a+x pattern), and you 
can use it to perform your own tests:

$ ./pattern ' *' ' hello'
Matching against ' *' :
hello: Match.

$ ./pattern ' hello*' ' hello' ' hello, there' ' well, hello'
Matching against ' hello*' :
hello: Match.
hello, there: Match.
well, hello: No match.
Remember to use single quotes around the arg

shell command line looping (tested on cygwin)


% for file in *.bar ; do > echo $file > 
mv $file `echo $file | sed 's/\.bar$/.foo/'` > done
% for file in `cat file_list` ; do > echo $file >
mv `echo $file | sed 's/\\r//'` `echo $file | sed 's/\.bar$/.foo/'` > done

uniq

An example: To see the list of lines in a file, sorted by the number of times each occurs:

sort file | uniq -c | sort -n

Source

Wednesday, October 20, 2010

The Pragmatic Programmer

4 key take-aways : 

1. Design with concurrency in mind.
 - i.e. assume that you are designing a multi threaded application
even though you may be designing a single threaded app.
It helps you to design cleanly and avoid temporal coupling.

Temporal coupling -> a;b; works but b;a; does not.
i.e. order of execution is critical.

An application designed in such a way(threadsafe) will
scale better, and if need be , can be adapted to multi-threaded
environments.

2. Get better at command line tools : awk/sed/gawk/for each/perl/grep etc.

3. Learn a new programming language every year

4. Read a technical book every quarter.

Tuesday, October 19, 2010

Thursday, October 14, 2010

Actionscript singleton class template IntelliJ Idea

Thanks to Gaurav Kushwaha, here is a singleton class template
for actionscript which you can use with Intellij Idea : 


package ${PACKAGE_NAME}#if (${PACKAGE_NAME} != "") #end
{
public class ${NAME} #if (${Super_class_name} != "")extends ${Super_class_name}#end{
private static var _instance:${NAME};

public static function getInstance():${NAME} {
    if (_instance == null) {
        _instance = new ${NAME}(new Dummy());
    }
    return _instance;
}
public function ${NAME}(dummy:Dummy) {
}

}
}
class Dummy {
    public function Dummy() {
}
}

sorting lines with vim

For e.g.

test.txt : 
z
y
x
w
v
u

If I say ->
:1,4 sort

test.txt will become
w
x
y
z
u
v

:.,+3 sort
will sort from current line to current line + 3.

:1,$ sort u
will sort and remove duplicates

To sort in reverse,
sort!

PS: This functionality doesn't work with IntelliJ Idea's vim plugin

Shell sample commands

Need to create a list of all the unique package names explicitly imported
by your Java code? The following stores it in a file called "list."

grep ' ^import ' *. j ava |
sed -e' s/. *import *//' -e's/; . *$//' |
sort -u >list


(sed -e is used for giving multiple commands to sed)

Shell sample commands


Which Java files have not been modified in the last week?
find . -name ' *. java' -mtime +7 -print


Which Java files have been modified in the last week?
find . -name ' *. java' -mtime -7 -print

Finding all files newer than a given file

find all c files newer than the Makefile : 

find . -name ' *. c' -newer Makefile -print

Problems in running shell scripts on cygwin

I copied pasted this code : 
#!/usr/bin/sh
N=1
while test "$N" -le "10"
do
echo "Number $N"
  N=$[N+1]
done

in test.sh.

Then ran it like : 
./test.sh

To get : 
./test.sh: line 7: syntax error: unexpected end of file

Issue was that Windows/Dos files don't work well
as shell scripts, due to difference in new line formatting.

So, the solution is , dos2Unix convert command : 
d2u test.sh

And, that's all.
Run happily ever after.

extracting social security number from an xml file using grep and cut

file.txt
<field10>123-45-6789</field10>
<field10>123-45-6789</field10>
<field10>123-4-6789-</field10>

here is the regexp

([0-9]+-)+[0-9]+<
and the command is 
grep -o '\([0-9]\+-\)\+[0-9]\+<' file.txt | cut -d "<" -f 1

Wednesday, October 13, 2010

Linting php files from command line in Cygwin

$ ls -1 *.php | xargs -n1 ./php.exe -l | grep -i "errors parsing"

Executing vim commands from command line

Source

I executed the following when I wanted to replace
all "include" statements in my php files to "include_once"

vim.exe -c "argdo %s/include /include_once /ge | update" *.php

Friday, October 8, 2010

bookmarklet for changing a page's max size

 javascript:(function(){var%20newSS,%20styles='body%20{max-width:%20800px;}';%20if(document.createStyleSheet)%20{document.createStyleSheet(%22javascript:'%22+styles+%22'%22);}%20else%20{newSS=document.createElement('link');%20newSS.rel='stylesheet';%20newSS.href='data:text/css,'+escape(styles);%20document.documentElement.childNodes[0].appendChild(newSS);}})();

Flex 4 difference between property and class

Source
Now, how do you know, regardless of the defnition style, which is a property
and which is a class?




   1:  <s:Group>
   2:    <s:layout>
   3:     <s:VerticalLayout/>
   4:    </s:layout>
   5:    <s:Button label="1"/>
   6:    <s:Button label="2"/>
   7:    <s:Button>
   8:     <s:label>3</s:label>
   9:    </s:Button>
  10:  </s:Group>

The G in the <s: Group> tag is uppercase, so it refers to an instance of a Flex class named  Group. The l in the <s:layout> tag is lowercase, so it is a property of the Group. The V in the  <s:VerticalLayout> tag is uppercase, so it is referring to a new instance of a Flex class named  VerticalLayout.

Scrolling content in Flex 4



Source
In previous versions of Flex, every container had this functionality by 
default. While extremely convenient for the developer, it also meant that 
every container was burdened with this extra code even though it was 
hidden the vast majority of times. In Flex 4, you need to specifically
indicate when an area is scrollable. Tis is accomplished via a special 
tag named Scroller that wraps your Group tag.

   1:  <s:Scroller height="65">
   2:    <s:Group>
   3:     <s:layout>
   4:      <s:VerticalLayout/>
   5:    </s:layout>
   6:     <s:Button label="1"/>
   7:     <s:Button label="2"/>
   8:     <s:Button label="3"/>
   9:    </s:Group>
  10:  </s:Scroller>

Flex 4 basic layouts


Source
   1:  <s:Group>
   2:    <s:layout>
   3:     <s:HorizontalLayout/>
   4:    </s:layout>
   5:    <s:Button label="1"/>
   6:    <s:Button label="2"/>
   7:    <s:Button label="3"/>
   8:  </s:Group>

HorizontalLayout/VerticalLayout/TileLayout/BasicLayout

AS-3/Flex Dictionary weak keys


A sample source code for using a dictionary in AS-3/Flex with weak keys.
When I say new Dictionary(true), after a while all those objects, whose
only references are as the dictionary keys, will be garbage collected.
So, if run the following in debug mode, after a while, output of trace 
will be 0, from 1 initially. 

   1:  <?xml version="1.0"?>
   2:  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()">
   3:      <mx:Script><![CDATA[
   4:          private var di:Dictionary = new Dictionary(true);
   5:          private function init():void {
   6:             di[new Sprite()] = [1,2,3];
   7:              setTimeout(startTrack,5000);
   8:          }
   9:   
  10:          private function startTrack():void {
  11:             System.gc();
  12:              var count:int = 0;
  13:              for each (var arr:Array in di) {
  14:                  ++count;
  15:              }
  16:              trace("number of elements in dictionary = " +count);
  17:              setTimeout(startTrack,5000);
  18:          }
  19:          ]]></mx:Script>
  20:  </mx:Application>

Sunday, October 3, 2010

Blog Archive