Archive for the ‘ Programming ’ Category

#Perl Parsing CSV

Today, I encounter a CSV parsing problem. The CSV file contains a comma inside double quote (").
for instance,
"Nur,Aini",1,2
The desired output parsing is
"Nur,Aini" 1 2
Here is a perl snippet to parse the above CSV file
perl -MText::ParseWords -nle' @kata=parse_line(",", 1, $_)
$|=1
foreach(@kata) {
printf ("%s\t",$_)
}
print "\n" ' filename
Iklan

#ShellScript Timeout Snippet

Just stick a great snippet that implements Timeout in the shellscript command 🙂

timeout=5                       # in seconds
ping 127.0.0.1 & cmdpid=$!      # Command to terminate
# Start "watchdog" process to terminate the command
# after $timeout seconds:
(sleep $timeout; kill -9 $cmdpid) &
watchdogpid=$!
wait $cmdpid                    # wait for command
kill $watchdogpid >/dev/null 2>&1

Courtesy : http://www.oase-shareware.org/shell/scripts/quickies/timeout

JDK Vs JRE Version

Got stuck while running an executable jar due to different version of JRE.
Unsupported major.minor version 51.0

Hence, I have to figure out what version of the JRE that I am currently using in my box.

nurrak@nbtplan002:/tmp$ java -version
java version "1.6.0_27"
OpenJDK Runtime Environment (IcedTea6 1.12.6) (6b27-1.12.6-1ubuntu0.12.04.4)
OpenJDK Server VM (build 20.0-b12, mixed mode)

then, I have to modify my current java version by using following command :

sudo update-alternatives --config java

Join Multiple Files #AWK

I am in love AWK recently 😀 get rid of Java, Perl etc in somewhere else.  AWK is able to process files at once quickly. I have posted an example to join two files,  now, I will show you how to process 3 files which are related each other. For instance, we have 3 files data1, data2, data3

data1

1 A
2 C

data2

2
3

data3

1 XXX
2 YYY

Displaying data3 based on key in data1 and data2 can be done as follows

awk '{
if(FNR ==1) f++
if(NR==FNR){ref[$1]=$2;next}
else if(f==2){ref2[$1]=1;next}

if(ref[$1] && ref2[$1] )
{
print ref[$1] " " $2
}
}
' data1 data2 data3 

One To Many Join #AWK

data1 contains a list of key

1 A,B
2 C

data2

1 XXX
2 YYY

We want to combine data1 and data2 by using column1 and display the results like this :

A XXX
B XXX
C YYY

The AWK script should be :

awk 'NR==FNR{ref[$1]=$2;next} 
{
if(ref[$1]) 
{
n=split(ref[$1],column2,",") 
for (i in column2) print column2[i] " " $2 
} 
}' data1 data2

[R] Trendlines Example

# set the margins
tmpmar <- par("mar")
tmpmar[3] <- 0.5
par(mar=tmpmar)

# get underlying plot
x <- 1:10
y <- jitter(x^2)
plot(x, y, pch=20)

# basic straight line of fit
fit <- glm(y~x)
co <- coef(fit)
abline(fit, col="blue", lwd=2)

# exponential
f <- function(x,a,b) {a * exp(b * x)}
fit <- nls(y ~ f(x,a,b), start = c(a=1, b=1))
co <- coef(fit)
curve(f(x, a=co[1], b=co[2]), add = TRUE, col="green", lwd=2)

# logarithmic
f <- function(x,a,b) {a * log(x) + b}
fit <- nls(y ~ f(x,a,b), start = c(a=1, b=1))
co <- coef(fit)
curve(f(x, a=co[1], b=co[2]), add = TRUE, col="orange", lwd=2)

# polynomial
f <- function(x,a,b,d) {(a*x^2) + (b*x) + d}
fit <- nls(y ~ f(x,a,b,d), start = c(a=1, b=1, d=1))
co <- coef(fit)
curve(f(x, a=co[1], b=co[2], d=co[3]), add = TRUE, col="pink", lwd=2)
legend("topleft",
    legend=c("linear","exponential","logarithmic","polynomial"),
    col=c("blue","green","orange","pink"),
    lwd=2,
    )

Result :
trendThis code is taken from http://stackoverflow.com/questions/15102254/how-do-i-add-different-trend-lines-in-r

[R] Boxplot with Data From file

For instance we have following data :
A,1,2,3
B,3,4,5
C,5,2,3
D,5,7,2

arguments <- commandArgs(TRUE)
mydata <- read.csv(file=arguments[1],head=FALSE,sep=",")
data <- t(mydata[,c(2:ncol(mydata))])
datay <- t(mydata[1])
boxplot( data  , main="Contoh Boxplot", xlab="data X", ylab="data Y",xaxt = "n")
#change x axis label
axis(1, at = 1:nrow(mydata), labels = datay)

Result :
boxplot