Created
September 11, 2017 10:56
-
-
Save mmahmoudian/7d37f845eede4ec42a4b5aa5a84e4bb8 to your computer and use it in GitHub Desktop.
A code to make life easier for those who want to view the data with different delimiters in terminal, even with massive files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# This file is useful for those who work in data science. For those | |
# who want to view the data with different delimiters in terminal. | |
# for those who have massive datasets and need to view some first | |
# few lines. | |
# | |
# Author: Mehrad Mahmoudian | |
# Contact: | |
# email: m[dot]mahmoudian[at]gmail[dot]com | |
# twitter: @mmahmoudian | |
# Date: 2017.09.11 (yyyy.mm.dd) | |
# version: 1.2.0 | |
# | |
# What this file do: | |
# This file inspects the file extention and present the file in | |
# propper format in terminal. For example it shows CSV files in | |
# eye-friendly and human readable format: | |
# 1 Year month Win8 Win7 Vista NT* WinXP Linux Mac Mobile | |
# 2 2011 201101 0 31.1 8.6 1 45.3 5 7.8 0.7 | |
# 3 2011 201102 0 32.2 8.3 1 44.2 5.1 8.1 0.7 | |
# 4 2011 201103 0 34.1 7.9 0.9 42.9 5.1 8 0.7 | |
# 5 2011 201104 0 35.9 7.6 0.9 40.9 5.1 8.3 0.8 | |
# | |
# Usage: | |
# You can use the shell as is: | |
# sh view.sh mydata.csv | |
# | |
# Add to alias: | |
# add the following line to the end of your aliases: | |
# alias view="sh path_to_your_shells/view.sh" | |
# remember to substitute the path_to_your_shells with the correct | |
# directory. | |
# | |
## initialize variables | |
FILE_VAL='' | |
OPEN_IN_CALC=FALSE | |
EDITABLE=FALSE | |
N_FIRST_ROWS=FALSE | |
SEPARATOR='' | |
## get the parameters | |
while getopts f:cen:h opts; do | |
case ${opts} in | |
h) | |
echo "Options" | |
echo " -f The file you want to view" | |
echo " -e Open in terminal edit mode using vim or nano" | |
echo " -c Open with LibreOffice Calc " | |
echo " -n The n number of first lines to be viewed. This is useful for big files" | |
;; | |
f) | |
FILE_VAL=${OPTARG} | |
;; | |
# if user wants to open in LibreOffice Calc | |
c) | |
OPEN_IN_CALC=TRUE | |
;; | |
# if user wants to open in an EDITABLE terminal environment | |
e) | |
EDITABLE=TRUE | |
;; | |
# number of n to show the first n number of lines | |
n) | |
N_FIRST_ROWS=${OPTARG} | |
;; | |
esac | |
done | |
# if user has not defined any arguments and only has provided the file | |
if [ ${FILE_VAL} = '' ]; then | |
FILE_VAL=${1} | |
fi | |
# check if the provided file exists | |
if [ ! -f ${FILE_VAL} ]; then | |
echo "Such file does not exist!" | |
exit | |
fi | |
# extract the file extention | |
EXTENTION=$(echo ${FILE_VAL} | rev | cut -d '.' -f 1 | rev | tr '[:upper:]' '[:lower:]') | |
# check the file extention | |
if [ "${EXTENTION}" = "csv" ]; then | |
SEPARATOR=',' | |
elif [ "${EXTENTION}" = "tsv" ]; then | |
SEPARATOR=' ' | |
else | |
echo ":!: File extention is unknown to this code and hence can not be processed!" | |
read -p "Press [Enter] key to see few first lines of the file..." | |
head ${FILE_VAL} | |
read -p "What separator is in use? " SEPARATOR | |
fi | |
# if user wants to open in LibreOffice Calc | |
if [ "${OPEN_IN_CALC}" = TRUE ]; then | |
# if user has not defined how many lines he/she wants | |
if [ "${N_FIRST_ROWS}" = FALSE ]; then | |
# create a pipe | |
cat ${FILE_VAL} > pipe | |
else | |
# create a pipe | |
head -n ${N_FIRST_ROWS} ${FILE_VAL} > pipe | |
fi | |
# open the pipe in calc | |
libreoffice --calc pipe | |
# remove the created pipe | |
rm pipe | |
# if user wants to open in an EDITABLE terminal environment | |
elif [ "${EDITABLE}" = TRUE ]; then | |
# check if vim installed | |
if type vim >/dev/null ; then | |
# if it was tab or space separated, we don't need to define it for column command | |
if [ "${SEPARATOR}" = ' ' ]; then | |
column -t < ${FILE_VAL} | vim - | |
else | |
column -s ${SEPARATOR} -t < ${FILE_VAL} | vim - | |
fi | |
# check if nano installed | |
elif type nano >/dev/null ; then | |
# if not installed, open with less | |
column -s ${SEPARATOR} -t < ${FILE_VAL} | less -#2 -N -S | |
else | |
(>&2 echo "Error: vim or nano are not installed! You can only view the file.") | |
exit 0 | |
fi | |
# if user wans to open it as read only | |
else | |
if [ "${SEPARATOR}" = '' ]; then | |
less -#2 -N -S ${FILE_VAL} | |
else | |
column -s ${SEPARATOR} -t < ${FILE_VAL} | less -#2 -N -S | |
fi | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment