Awk scripts to display a CSV file in a shell terminal with readable columns and lines, like with Mysql command line. Also offers methods to filter or aggregate the CSV file by line or column, count data inside and sum the column values.
In default display mode, all columns are on one line. You can use the first line as header or/and the last as footer. With the vertical mode, each column has it own line. All both modes can deal with lines with comma inside quotes.
With the following CSV file name sample.csv
as source.
Day,Campaign ID,Campaign,Clicks,Impressions,Cost,Mobile Url,Campaign state,Tracking template
2015-10-01,1234567890,@1 #sp,11,297,4420000,,paused,
2015-10-20,1234567890,@1 #sp,17,1242,17170000,,paused,https://github.com/rvflash
2015-10-22,123,@3 #sp,10,854,13290000,,paused,
2015-10-24,1234567890,@1 #sp,4,547,4740000,,paused,
2015-10-26,123456,"@2 ,#sp",4,577,1310000,,paused,
$ awk -f termTable.awk tests/unit/sample.csv
+------------+-------------+----------+--------+-------------+----------+------------+----------------+----------------------------+
| Day | Campaign ID | Campaign | Clicks | Impressions | Cost | Mobile Url | Campaign state | Tracking template |
+------------+-------------+----------+--------+-------------+----------+------------+----------------+----------------------------+
| 2015-10-01 | 1234567890 | @1 #sp | 11 | 297 | 4420000 | | paused | |
| 2015-10-20 | 1234567890 | @1 #sp | 17 | 1242 | 17170000 | | paused | https://github.com/rvflash |
| 2015-10-22 | 123 | @3 #sp | 10 | 854 | 13290000 | | paused | |
| 2015-10-24 | 1234567890 | @1 #sp | 4 | 547 | 4740000 | | paused | |
| 2015-10-26 | 123456 | @2 ,#sp | 4 | 577 | 1310000 | | paused | |
+------------+-------------+----------+--------+-------------+----------+------------+----------------+----------------------------+
$ awk -v verticalMode=1 -f termTable.awk tests/unit/sample.csv
**************************** 1. row ****************************
Day: 2015-10-01
Campaign ID: 1234567890
Campaign: @1 #sp
Clicks: 11
Impressions: 297
Cost: 4420000
Mobile Url:
Campaign state: paused
Tracking template:
**************************** 2. row ****************************
Day: 2015-10-20
Campaign ID: 1234567890
Campaign: @1 #sp
Clicks: 17
Impressions: 1242
Cost: 17170000
Mobile Url:
Campaign state: paused
Tracking template: https://github.com/rvflash
**************************** 3. row ****************************
Day: 2015-10-22
Campaign ID: 123
Campaign: @3 #sp
Clicks: 10
Impressions: 854
Cost: 13290000
Mobile Url:
Campaign state: paused
Tracking template:
**************************** 4. row ****************************
Day: 2015-10-24
Campaign ID: 1234567890
Campaign: @1 #sp
Clicks: 4
Impressions: 547
Cost: 4740000
Mobile Url:
Campaign state: paused
Tracking template:
**************************** 5. row ****************************
Day: 2015-10-26
Campaign ID: 123456
Campaign: @2 ,#sp
Clicks: 4
Impressions: 577
Cost: 1310000
Mobile Url:
Campaign state: paused
Tracking template:
More options are available for termTable.awk
, see list below:
verticalMode
, enable vertical display tablewithFooter
, use the last line as footer. Option disabled with verticalModewithoutHeader
, disable the first line as header. Option disabled with verticalModeaddHeader
, line to add as header with column's names separated by commareplaceHeader
, The non-empty values separated by comma overloads the column's namescolumnSeparator
, character to separate column, default "|"columnBounce
, character to bounce separate line, default "+"columnBreakLine
, character to use as break line, default "-" or "*" in vertical modelineLabel
, string to use as suffix of the line number in vertical mode
$ awk -v replaceHeader="Date,,Name" -f termTable.awk tests/unit/sample.csv
+------------+-------------+---------+--------+-------------+----------+------------+----------------+----------------------------+
| Date | Campaign ID | Name | Clicks | Impressions | Cost | Mobile Url | Campaign state | Tracking template |
+------------+-------------+---------+--------+-------------+----------+------------+----------------+----------------------------+
| 2015-10-01 | 1234567890 | @1 #sp | 11 | 297 | 4420000 | | paused | |
| 2015-10-20 | 1234567890 | @1 #sp | 17 | 1242 | 17170000 | | paused | https://github.com/rvflash |
| 2015-10-22 | 123 | @3 #sp | 10 | 854 | 13290000 | | paused | |
| 2015-10-24 | 1234567890 | @1 #sp | 4 | 547 | 4740000 | | paused | |
| 2015-10-26 | 123456 | @2 ,#sp | 4 | 577 | 1310000 | | paused | |
+------------+-------------+---------+--------+-------------+----------+------------+----------------+----------------------------+
Exclude columns named Day, Mobile Url, Campaign state and Tracking template.
$ awk -v columns="2 3 4 5 6" -f limit.awk tests/unit/sample.csv | awk -f termTable.awk
+-------------+----------+--------+-------------+----------+
| Campaign ID | Campaign | Clicks | Impressions | Cost |
+-------------+----------+--------+-------------+----------+
| 1234567890 | @1 #sp | 11 | 297 | 4420000 |
| 1234567890 | @1 #sp | 17 | 1242 | 17170000 |
| 123 | @3 #sp | 10 | 854 | 13290000 |
| 1234567890 | @1 #sp | 4 | 547 | 4740000 |
| 123456 | @2 ,#sp | 4 | 577 | 1310000 |
+-------------+----------+--------+-------------+----------+
Available options for limit.awk
, see list below:
withHeader
, use first as headercolumns
, list of columns by index to display, separated by spacerowOffset
, starting offset for rows limitrowCount
, number of line requested in limit
Group the sample datas by Campaign ID and sum the clicks, impressions and cost.
$ awk -v columns="2 3 4 5 6" -f limit.awk tests/unit/sample.csv | awk -v groupByColumns="1" -v sumColumns="3 4 5" -v omitHeader=1 -v header="Campaign Id,Name,Sum of clicks, Sum of impression, Sum of cost" -f aggregate.awk | awk -f termTable.awk
+-------------+---------+---------------+-------------------+-------------+
| Campaign Id | Name | Sum of clicks | Sum of impression | Sum of cost |
+-------------+---------+---------------+-------------------+-------------+
| 1234567890 | @1 #sp | 32 | 2086 | 26330000 |
| 123456 | @2 ,#sp | 4 | 577 | 1310000 |
| 123 | @3 #sp | 10 | 854 | 13290000 |
+-------------+---------+---------------+-------------------+-------------+
Available options for aggregate.awk
, see list below:
header
, define new header lineomitHeader
, use to exclude the first linedistinctLine
, aggregate by the entire lineavgColumns
, list of columns per index on which to calculate the average, separated by spacedistinctColumns
, list of columns by index to use in single modecountColumns
, list of columns by index to count, separated by spacemaxColumns
, list of columns per index where find the max value, separated by spaceminColumns
, list of columns per index where find the min value, separated by spacesumColumns
, list of columns by index to sum, separated by spacegroupByColumns
, list of columns by index to use to group, separated by space
The length() function is not supported in mawk, but I use it in these scripts. So prefered instead gawk, nawk or original awk. To find the current awk version, use the following command :
awk -Wversion 2>/dev/null || awk --version
On MacOs, you can use brew to install it: brew install gawk
On Debian based GNU/Linux, use APT package manager: sudo apt-get update; sudo apt-get install gawk