Wednesday, May 8, 2013

Unix - Append 0 to single digit date


Input file file.txt has dates in month/day/year format.
$ cat file.txt 
3/4/2013
3/10/2013
10/4/2013
12/10/2012
Required: Add prefix 0 to first and second field if its a single digit.

Awk solution:
$ awk 'BEGIN {FS=OFS="/"} 
    { 
 if (length($1) == 1) $1="0"$1
 if (length($2) == 1) $2="0"$2
        { print }
}' file.txt
Output:
03/04/2013
03/10/2013
10/04/2013
12/10/2012
Related posts:
- A newbie tutorial on Unix Awk
- Awk if else
- Convert date format in unix using awk and sed

Thursday, February 28, 2013

UNIX time command output redirect to file

As you know the 'time' command run programs and summarize system resource usage. Here is a way how you can redirect 'time' command output to file. I would recomend you to go through this page to understand more about 'time' command.

e.g. We are executing 'prog.sh' along with 'time' command.
$ time ./prog.sh 
Initiating merge
Merge completed

real	0m8.803s
user	0m0.010s
sys	0m0.000s
#Trying to redirect the output to a file
$ time ./prog.sh > out.txt

real	0m8.804s
user	0m0.020s
sys	0m0.000s
#out.txt content:
$ cat out.txt 
Initiating merge
Merge completed
#So it only redirected the STDOUT of the script executed, but the 'time' command outut is not redirected.
#This is becuase the command time sends it's output to STDERR (instead of STDOUT)
#To capture output of 'time' command:
$ { time ./prog.sh ; } 2> out.txt
Initiating merge
Merge completed
#Now out.txt content:
$ cat out.txt 

real	0m8.303s
user	0m0.010s
sys	0m0.000s
#And to capture output of script as well as time command:
$ { time ./prog.sh ; } &> out.txt
#'out.txt' now has both the outputs.
$ cat out.txt 
Initiating merge
Merge completed

real	0m8.303s
user	0m0.020s
sys	0m0.000s
As I have mentioned in my earlier post on UNIX 'time' command, there's two types of time command available:
1) Shell's in-build time: Gives only scheduler information
2) /usr/bin/time: Gives more information, also allows formatting the output

The second (/usr/bin/time) one accepts output redirection without code block:
$ /usr/bin/time ./prog.sh &> newout.txt

$ cat newout.txt 
Initiating merge
Merge completed
0.00user 0.01system 0:08.30elapsed 0%CPU (0avgtext+0avgdata 5728maxresident)k
0inputs+8outputs (0major+719minor)pagefaults 0swaps
Related posts:
- UNIX redirect man pages to file
- Redirect UNIX top command output to file
- UNIX redirect both stderr and stdout to file
- Run UNIX bash loop with nohup command

Friday, February 15, 2013

Bash - convert delimited file to html table

Here is a simple UNIX bash script that can be used to convert a simple delimited file to a HTML table format. Default delimiter if not mentioned as part of the command line argument to this script will be comma.
#!/bin/sh
#convert_2_html_table.sh 
#Converts a delimited file to a HTML table
#Jadu Saikia http://unstableme.blogspot.in

NOARG=64

#usage function
f_Usage () {
echo "Usage: $(basename $0) -d <delimiter> -f <delimited-file>"
}

#command line args
while getopts d:f: OPTION
do
    case $OPTION in
        d)  DELIMITER=$OPTARG ;;
        f)  INFILE=$OPTARG ;;
    esac
done

#Less than 2 command line argument, throw Usage
[ "$#" -lt 2 ] && f_Usage && exit $NOARG

DEFAULTDELIMITER=","
#If no delimiter is supplied, default delimiter is comma i.e. ,
SEPARATOR=${DELIMITER:-$DEFAULTDELIMITER}

if [ -f "${INFILE}" ]
        then
                printf "<table border=\"1\">"
                sed "s/$SEPARATOR/<\/td><td>/g" $INFILE | while read line
                        do
                                printf "<tr><td>${line}</td></tr>"
                done
                printf "</table>"
                echo
fi

e.g. Input file:
$ cat data.txt 
First Name:Last Name:Points
Alex:Hall:45
Niraj:Kumar:290
Brian:Smith:100
Executing it:
$ ./convert_2_html_table.sh -d ":" -f data.txt  > data.html
 
 Related posts:
- Setting default value for Bash shell variable
- Accessing external variable in SED and AWK
- Issue with Bash while loop during SUM

© Jadu Saikia http://unstableme.blogspot.com