Database Reference
In-Depth Information
but always include the header in the output? Or what if we only wanted to uppercase
the values of a specific column using tr and leave the other columns untouched?
There are multistep workarounds for this, but they're very cumbersome. We have
something better. In order to leverage ordinary command-line tools for CSV, we'd like
to introduce you to three command-line tools, aptly named: body (Janssens, 2014),
header (Janssens, 2014), and cols (Janssens, 2014).
Let's start with the first command-line tool: body . With body , you can apply any
command-line tool to the body of a CSV file (i.e., everything excluding the header).
For example:
$ echo -e "value\n7\n2\n5\n3" | body sort -n
value
2
3
5
7
It assumes that the header of the CSV file only spans one row. Here's the source code
for completeness:
#!/usr/bin/env bash
IFS = read -r header
printf '%s\n' "$header"
$@
It works like this:
Take one line from standard input and store it as a variable named $header .
Print out the header.
Execute all the command-line arguments passed to body on the remaining data.
Here's another example. Imagine that we count the lines of the following CSV file:
$ seq 5 | header -a count
count
1
2
3
4
5
With wc -l , we can count the number of all lines:
$ seq 5 | header -a count | wc -l
6
If we only want to consider the lines in the body (so everything except the header),
we simply add body :
Search WWH ::




Custom Search