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

Feature request: rename columns when selecting #173

Open
purcell opened this issue Apr 3, 2019 · 8 comments
Open

Feature request: rename columns when selecting #173

purcell opened this issue Apr 3, 2019 · 8 comments

Comments

@purcell
Copy link

purcell commented Apr 3, 2019

Since xsv is so invaluable for splitting and reformatting big messy CSV files (thanks!), it'd be really useful to be able to rename columns when select-ing them out: I deal with some crappy CSV files that have 150-character header names.

At first I was thinking a special syntax for select might be the solution, but now I suspect a rename subcommand might be the more appropriate option. Not really up to speed with Rust dev yet, so starting off by sharing this idea for feedback.

@BurntSushi
Copy link
Owner

This sounds like a possibly good idea to me. It probably should be a separate rename command. It will need some design work in terms of the syntax supported. It would ideally combine select syntax for specifying which columns to rename, for example.

@purcell
Copy link
Author

purcell commented Apr 3, 2019

Yeah, I'm thinking something like 1=New name,Code=value,Dupe[3]="Nondupe","Quoted[2]"=something. There's no need for the 1-4 column range syntax or ! exclusion syntax for a pure rename command, which makes this simpler than cramming renaming into select.

@BurntSushi
Copy link
Owner

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.

@purcell
Copy link
Author

purcell commented Apr 3, 2019

Ah, yes. I can see that might work. The simple case would then just be xsv rename "Foo bar" "Fubar", which is nice.

I had a look through past issues etc., and what we're talking about here sounds a bit like the --set-headers suggestion which was part of #96, but a bit cleaner IMO.

@kacrouse
Copy link

kacrouse commented Apr 5, 2019

+1 for this feature! Definitely have had the need for this a few times.

@jlumbroso
Copy link

@BurntSushi @purcell @kacrouse I had to merge/join CSV files today, and I just stumbled upon xsv and it's a really neat project!

I really needed to be able to rename columns so I tried to figure out what would be the best way to contribute a solution. I implemented what was discussed in this thread, specifically the variant suggested by @BurntSushi. Let me know if this hits the spot!

@jillesvangurp
Copy link

jillesvangurp commented May 23, 2019

a sql like syntax for this would be nice. So something like xsv select "personName as name,user_id as id" users.csv

@jillesvangurp
Copy link

I appreciate that you don't want to support the full sql syntax. I already use csvsql (comes with csvkit), which indeed uses sqlite. My main issue with that is that it is kind of slow compared to xsv, which is awesome.

The reason I proposed this AS syntax is because it is an elegant solution with minimal impact that is easy to understand and probably also to implement and pretty.

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

No branches or pull requests

5 participants