Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the best way to print line number as the first column #170

Closed
san-r opened this issue Oct 19, 2021 · 4 comments
Closed

What is the best way to print line number as the first column #170

san-r opened this issue Oct 19, 2021 · 4 comments

Comments

@san-r
Copy link

san-r commented Oct 19, 2021

This is not an issue but question. As of now, csvtk sub-commands supporting the flag -n or --line-number to "print line number as the first column ("n")" are as under:

grep
filter
filter2
sample

One way to print line number as the first column at the end of workflow is as under:

csvtk sub-command ... |^
csvtk another-sub-command ... |^

csvtk grep -n -f1 -r -p ".*" |^
csvtk pretty

I'm wondering if there a better (more processor and RAM friendly) way to to print line numbers than using
csvtk grep -n -f1 -r -p ".*"

@shenwei356
Copy link
Owner

I don't think making all commands support this is a good idea.

Maybe adding a new command cat with a -n option to print the line numbers.

@san-r
Copy link
Author

san-r commented Oct 19, 2021

Using a sub-command like grep in the manner shown above just to print line numbers is certainly inefficient. And yes, only one sub-command cat supporting -n to print line numbers will be excellent. One more thing I've noticed is that cat sub-command does not support gzipped input unlike other sub-commands. It would be nice for it to do so.

@shenwei356
Copy link
Owner

oh, @bsipos have already created acat, can we rename it to watch? like seqkit watch?

@shenwei356
Copy link
Owner

In the next version, you can easily use

$ csvtk cut -f 1- -Z testdata/names.csv 
row,id,first_name,last_name,username
1,11,Rob,Pike,rob
2,2,Ken,Thompson,ken
3,4,Robert,Griesemer,gri
4,1,Robert,Thompson,abc
5,NA,Robert,Abel,123

$ csvtk filter2 -f '$id>2' -n  testdata/names.csv 
row,id,first_name,last_name,username
1,11,Rob,Pike,rob
3,4,Robert,Griesemer,gri

Related changes:

  • add a new global flag -Z/--show-row-number, supported commands: cut, csv2tab, csv2xlsx, tab2csv, pretty.
  • the colum name of row number changes from "n" to "row": csv2xlsx, csv2tab, cut, filter, filter2, grep, pretty, sample, tab2csv.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants