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

New "rename" command to change column header names #175

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

jlumbroso
Copy link

This addresses issue #173 by introducing a new verb/command "rename" which allows renaming of all selected columns.

Implemented following exactly @BurntSushi's recommendations:

A simpler version might be too just keep the same select syntax, which selects some number of columns in a particular order. Then the remaining positional arguments are the new names of the columns, where there are exactly N arguments, where N is the number of fields selected.

The general syntax is:

xsv rename [options] [--] <selection> <renamed>... [<input>]

where first you select columns with the same syntax as the select command, and then you must provide exactly as many positional arguments to rename the selected columns. Of course, this includes the ability to copy a column multiple times with different names.

Here are some usage examples:

  • Assume a file example1.csv contains the following:
    header1,header2
    row1H1,row1H2
    row2H1,row2H2
    
  • Here is how we rename both columns showing different variants with the selection syntax:
    $ xsv rename 'header1,header2' H1 H2 example1.csv
    H1,H2
    row1H1,row1H2
    row2H1,row2H2
    
    $ xsv rename 1-2 H1 H2 example1.csv
    H1,H2
    row1H1,row1H2
    row2H1,row2H2
    
    $ xsv rename 1,1,1,2 H1a H1b H1c H2 example1.csv
    H1a,H1b,H1c,H2
    row1H1,row1H1,row1H1,row1H2
    row2H1,row2H1,row2H1,row2H2
    
    $ xsv rename 1-2 H1 example1.csv
    While renaming, selected 2 columns, but only provided 1 new names.
    

This also addresses one of the points of issue #96, while being not as powerful but probably cleaner/easier to compose with other operations.

AFAIK, compilation produced no warnings; I tried to respect conventions, encapsulations and modularization as much as possible. Please let me know how to improve this pull request.

@purcell
Copy link

purcell commented Apr 10, 2019

Nice! 🎉

@purcell
Copy link

purcell commented Apr 30, 2019

Checking back in on this, it occurs to me that the README might also need to be updated to list the new subcommand.

@shouze
Copy link

shouze commented Jun 12, 2019

@purcell @jlumbroso coming! jlumbroso#1

readme: add rename command
@jlumbroso
Copy link
Author

@purcell Sorry, had not seen your second comment, but you are quite right of course!
@shouze Merci Sébastien !

@geekscrapy
Copy link

Is this released? I just did a brew install and it's not included :)

@jlumbroso
Copy link
Author

@geekscrapy No, this was never merged into the codebase by @BurntSushi. Given the growing number of pull requests that are unanswered, the lack of commits, my guess is that the original maintainer no longer has any mental bandwidth for this project.

I created my own project to address my needs; perhaps you might find it useful:
https://github.com/jlumbroso/csv-merge

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

Successfully merging this pull request may close these issues.

None yet

4 participants