Monday, October 25, 2010

Taglist in shell

Tag list source code.


Shell word frequency filter

Also available here .

# 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
cat fileName.txt | ./ 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•'

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

number of words used exactly once


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


#! /bin/sh
# Convert a tab-separated value file to grammar-conformant HTML.
# Usage:
#    tsv-to-html < infile > outfile
cat << EOFILE Leading boilerplate
            Office directory
        <LINK REV="made" HREF="mailto: $USER@` hostname` ">
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

a fantastic resource for unix command line examples

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

$ cat test.txt

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

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

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. "; ;

[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. "; ;
The only fruit for which there is no Cockney slang.

shell case switch

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

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


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


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

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 : 

If I say ->
:1,4 sort

test.txt will become

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

:1,$ sort u
will sort and remove duplicates

To sort in reverse,

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 : 
while test "$N" -le "10"
echo "Number $N"


Then ran it like : 

To get : 
./ 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 : 

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

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


here is the regexp

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


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


Flex 4 difference between property and class

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

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

   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>


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="" 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:          }
  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