Skip to main content

CSV to HTML Converter Shell Script

#Generic Converter from CSV to HTML #!/bin/bash usage() { cat <<EOF Usage:$(basename $0)[OPTIONS] input_file > ouptut.html Explicit Delimiter can be specified if not then it default to comma as delimiter Options: -d specify delimiter , instead of comma --head specified then treats first line as column header , <thead> and <th> tags --foot last line , <tfoot> and <th> tags Samples: 1.$(basename $0) input.csv Parse 'input.csv' with comma as delimiter and send HTML table to STDOUT 2. $(basename $0) -d '|' < input.csv > out.html Parse 'input.csv' with PIPE as delimiter and send HTML table to out.html 3. $(basename $0) -d '\t' --head --foot < input.tsv > out.html Parse 'input.tsv' , tab as delimiter process first and last lines as header and footer write to out.html EOF } while true; do case "$1" in -d) shift d="$1" ;; --foot) foot="-v ftr=1" ;; --help) usage exit 0 ;; --head) head="-v hdr=1" ;; -*) echo "Error: Unknown Option '$1'"" echo "See '--help' for usage" exit 1 ;; *) f=$1 break ;; esac shift done if [ -z "$d" ];then d="," fi if [ -z "$f" ];then echo "Error : No Input file Specified" echo "See '--help' for more information" exit 1 fi if [ -f "$f" ];then echo "Error : File Specifed '$f' got corrupted and cannot be read" echo "See '--help' for more information" exit 1 else fileContent=$(sed '/^$/d' $f) last=$(wc -l <<< "$fileContent") fi awk -F "$d" -v last=$last $head $foot ' BEGIN { print " <HTML><table border=1>" } { gsub(/</,"\\&lt;") gsub(/>/,"\\&gt;") if(NR == 1 && hdr) { printf " <thead>\n" gsub(/&?,"\\&gt;") } if(NR == last && ftr) { printf " <tfoot>\n" } print " <tr>" for(f=1;f<=NF;f++){ if((NR == 1 && hdr)|| (NR == last && ftr)) { printf " <th>font color=blue><center>%s</center></font></th>\n",$f } else printf " <td>justify>%s</justify></td>\n",$f } print " </tr>" if (NR == 1 && hdr) { printf " </thead>\n" } if (NR == last && ftr) { printf " </tfoot>\n" } } END { print " </table></BODY></HTML>" } ' <<< "$fileContent"

Comments

Popular posts from this blog

LookML

  What Is LookML? LookML is a language for describing dimensions, aggregates, calculations, and data relationships in a SQL database. Looker uses a model written in LookML to construct SQL queries against a particular database. LookML Projects A LookML Project is a collection of model, view, and dashboard files that are typically version controlled together via a Git repository. The model files contain information about which tables to use, and how they should be joined together. The view files contain information about how to calculate information about each table (or across multiple tables if the joins permit them). LookML separates structure from content, so the query structure (how tables are joined) is independent of the query content (the columns to access, derived fields, aggregate functions to compute, and filtering expressions to apply). LookML separates content of queries from structure of queries SQL Queries Generated by Looker For data analysts, LookML fosters DRY style (“d

A Deep Dive Into Google BigQuery Architecture

Introduction Google’s BigQuery is an enterprise-grade cloud-native data warehouse. BigQuery was first launched as a service in 2010 with general availability in November 2011. Since inception, BigQuery has evolved into a more economical and fully-managed data warehouse which can run blazing fast interactive and ad-hoc queries on datasets of petabyte-scale. In addition, BigQuery now integrates with a variety of Google Cloud Platform (GCP) services and third-party tools which makes it more useful. BigQuery is serverless, or more precisely data warehouse as a service. There are no servers to manage or database software to install. BigQuery service manages underlying software as well as infrastructure including scalability and high-availability. The pricing model is quite simple - for every 1 TB of data processed you pay $5. BigQuery exposes simple client interface which enables users to run interactive queries. Overall, you don’t need to know much about underlying BigQuery architecture or