mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 23:28:02 +00:00
Add Count The Lines In A CSV Where A Column Is Empty as a Unix TIL
This commit is contained in:
25
unix/count-the-lines-in-a-csv-where-a-column-is-empty.md
Normal file
25
unix/count-the-lines-in-a-csv-where-a-column-is-empty.md
Normal file
@@ -0,0 +1,25 @@
|
||||
# Count The Lines In A CSV Where A Column Is Empty
|
||||
|
||||
The [`xsv` utility](https://github.com/BurntSushi/xsv) is a fast way to analyze
|
||||
and work with CSV files from the command line.
|
||||
|
||||
With the `search` subcommand, I can seach for lines that match a pattern and
|
||||
even narrow that search to focus on a selected column.
|
||||
|
||||
For instance, to search for any lines where column 3 is empty:
|
||||
|
||||
```
|
||||
$ xsv search -s 3 '^$' data.csv
|
||||
```
|
||||
|
||||
The `-s 3` narrows the search to just column 3. The `'^$'` regex pattern
|
||||
matches on cells where there is the start character (`^`) and end character
|
||||
(`$`) with nothing in between, hence empty.
|
||||
|
||||
I can then pipe that to `wc -l` to get a count of the number of empty lines.
|
||||
|
||||
```
|
||||
$ xsv search -s 3 '^$' data.csv | wc -l
|
||||
```
|
||||
|
||||
See `xsv search --help` for more details.
|
||||
Reference in New Issue
Block a user