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

Output column raw value #44

Closed
JamesColeman-LW opened this issue Apr 28, 2022 · 5 comments
Closed

Output column raw value #44

JamesColeman-LW opened this issue Apr 28, 2022 · 5 comments

Comments

@JamesColeman-LW
Copy link

The program jq offers an option to output the raw value which is really useful in bash scripting, it would be nice of gocsv also had this to output a column without CSV formatting (quotes and new lines as \n) to allow querying and extracting data.

@zacharysyoung
Copy link
Contributor

@JamesColeman-LW, will you please provide a sample CSV with the data you want to view as "raw", and how you expect it to look coming out of the command?

@JamesColeman-LW
Copy link
Author

sample.csv

It would be nice if I could do the following and add a raw value argument:

cat sample.csv | gocsv filter -c 2 -eq "af4ba48d_mage2[af4ba48d_mage2] @ localhost []" | gocsv filter -c 5 -eq "Query" | gocsv select -c 6

This is what I'm doing with the output right now.

cat /var/lib/mysql/mysql/general_log.CSV | gocsv filter -c 2 -eq "af4ba48d_mage2[af4ba48d_mage2] @ localhost []" | gocsv filter -c 5 -eq "Query" | gocsv select -c 6 | sed 's/^"//g' | sed 's/"$//g' | sed 's/$/;/g' | sed 's/\\n/\n/g'

@aotimme
Copy link
Owner

aotimme commented Jun 24, 2022

Yeah I think we could do this. And it makes sense to follow the same API and semantics as jq's --raw-output / -r flag. From https://stedolan.github.io/jq/manual/ (emphasis mine):

With this option, if the filter's result is a string then it will be written directly to standard output rather than being formatted as a JSON string with quotes.

So what I think this means for gocsv is:

  1. The --raw-output / -r would be an option only for the select subcommand.
  2. If the result of the select subcommand is just 1 column, it will output each cell as simple text (no quoting, etc.) if using the --raw-output / -r flag.
  3. If the result of the select subcommand is more than 1 column, then the --raw-output / -r flag will do nothing.

Does that API / semantics make sense to you? If we agree, then I can implement it or someone else if they're interested!

@JamesColeman-LW
Copy link
Author

That sounds reasonable to me.

aotimme added a commit that referenced this issue Dec 30, 2022
Follows #44 re the details:
* Uses the `--raw-output` / `-r` flag.
* Only applies to the `select` subcommand when selecting only a single column.
@aotimme
Copy link
Owner

aotimme commented Dec 30, 2022

Done in cf64ecc, now in release v1.0.2.

@aotimme aotimme closed this as completed Dec 30, 2022
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

3 participants