shenwei356 / csvtk

A cross-platform, efficient and practical CSV/TSV toolkit in Golang
http://bioinf.shenwei.me/csvtk
MIT License
999 stars 84 forks source link

[Suggestion] Add a tail command #194

Closed apcamargo closed 1 year ago

apcamargo commented 2 years ago

As far as I know, there's no way to easily inspect the last N records of a .csv file in a way that includes the header. I assume this could be achieved by reading the first line (the header) and skip to the last N lines of the file.

tetedange13 commented 1 year ago

Hi @apcamargo ,

I managed to do this with awk and tac (inspired from "awk Solution" of this answer : https://unix.stackexchange.com/a/139099)

Input CSV

==> test.csv <==
id,age,gender
1,39,M
2,25,M
3,36,F
4,45,F

One-liner

tac test.csv | awk -v nbr=2 'NR<=nbr { prev=$0; print } END { if($0!=prev){print} }' | tac

Output (pretty)

id   age   gender
--   ---   ------
3    36    F
4    45    F

Explanations

Hope this helps ! Have a nice day, Felix.

shenwei356 commented 1 year ago

Thank you @tetedange13