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.
Related posts:
- Setting default value for Bash shell variable
- Accessing external variable in SED and AWK
- Issue with Bash while loop during SUM
#!/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 fie.g. Input file:
$ cat data.txt First Name:Last Name:Points Alex:Hall:45 Niraj:Kumar:290 Brian:Smith:100Executing 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
1 comment:
We can also make use of the unix typesetting tools (tbl/nroff) for this task, like as,
< data.txt \
sed -e '
1i\
.TS\
center,allbox,tab(@);\
c c c\
l l n.
y/:/@/
$a\
.TE
' | tbl - | groff -T ascii -ms | sed -e '/./!d'
Post a Comment