BurntSushi / xsv

A fast CSV command line toolkit written in Rust.
The Unlicense
10.29k stars 317 forks source link

Can't select headers with dashes in them #251

Closed denizdogan closed 3 years ago

denizdogan commented 3 years ago

It seems that selection of any headers with a dash (-) in the name fails. If it's one dash as in A-B, it thinks I'm looking for A. If it's two dashes as in A-B-C, it errors out. With no dashes, it works fine.

$ xsv headers 19990101-30201124.csv
1   MessageHeader-MessageRecipient-PartyId
2   MessageHeader-MessageThreadId
3   MessageHeader-MessageControlType
4   MessageHeader-MessageCreatedDateTime
5   MessageHeader-MessageSender-PartyId
6   MessageHeader-MessageSender-FullName
7   MessageHeader-MessageRecipient-FullName
8   MessageHeader-MessageId
9   UpdateIndicator
$ xsv select MessageHeader-MessageThreadId 19990101-30201124.csv
Selector name 'MessageHeader' does not exist as a named header in the given CSV data.
$ xsv select MessageHeader-MessageRecipient-PartyId 19990101-30201124.csv
Expected end of field but got '-' instead.
$ xsv select "MessageHeader-MessageRecipient-PartyId" 19990101-30201124.csv
Expected end of field but got '-' instead.
$ xsv select UpdateIndicator 19990101-30201124.csv | head -n 1
UpdateIndicator
Yomguithereal commented 3 years ago

@denizdogan can you try double escaping?

xsv select '"MessageHeader-MessageRecipient-PartyId"' 19990101-30201124.csv
denizdogan commented 3 years ago

@Yomguithereal That seems to work, thanks! Is this the intended behavior?

Yomguithereal commented 3 years ago

I think so because, even if it is not ideal, this is probably the only way to make the column selection syntax (with -, ,, ! integer columns etc.) work well with column name escaping. This is what I found in the xsv select -h help message on the subject:

  Quote column names that conflict with selector syntax:
  $ xsv select '"Date - Opening","Date - Actual Closing"'
denizdogan commented 3 years ago

@Yomguithereal Thanks again, closing this :)

BurntSushi commented 3 years ago

Yes, it's intended behavior. It's documented right in the help output. - is a meta character in column selector syntax, so you have to quote it.