Skip to content

Instantly share code, notes, and snippets.

@dukechem
Last active February 10, 2020 19:46
Show Gist options
  • Save dukechem/50263bd73804f834eff9a19125242209 to your computer and use it in GitHub Desktop.
Save dukechem/50263bd73804f834eff9a19125242209 to your computer and use it in GitHub Desktop.
csvcol.sh displays how many rows & cols, and shows first 2 rows of csv file(s). Bash/sh script using head, tail, wc, and awk.
#!/bin/bash
if [ $# -eq 0 ]
then
bnam=$(basename "$0")
echo "Usage: $bnam *.csv (displays how many rows & cols, and shows first 2 rows of csv file(s))"; exit
fi
# EXAMPLE OUTPUT
# Total Rows Words Size = 76 76 5587 foobar.csv
# Line 1 has 20 Cols = MW,HBA,HBD,N,O,LOGP,ROTB,TPSA,LOGD,RINGS,ARRINGS,HETRINGS,SYSRINGS,SYSRR,FSP3,NSTEREO,ASA,RELPSA,TC,VWSA
# Line 2 has 20 Cols = 391.43,6,0,4.54,4,71.33,3.22,4,2,4,4,2,2,2,0.27,1,576.35,0.13,-1,538.32
for csv in "$@"
do
echo -n " Total Rows Words Size = "
wc "$csv"
head -1 "$csv" | awk -F, ' { if (NF >1) print " Line 1 has " NF " Cols = " $0; else print $0; }'
head -2 "$csv" | tail -1 | awk -F, ' { if (NF >1) print " Line 2 has " NF " Cols = " $0; else print $0; }'
echo ""
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment