gocsv icon indicating copy to clipboard operation
gocsv copied to clipboard

Output column raw value

Open JamesColeman-LW opened this issue 2 years ago • 4 comments

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.

JamesColeman-LW avatar Apr 28 '22 16:04 JamesColeman-LW

@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?

zacharysyoung avatar May 10 '22 16:05 zacharysyoung

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'

JamesColeman-LW avatar Jun 23 '22 15:06 JamesColeman-LW

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!

aotimme avatar Jun 24 '22 15:06 aotimme

That sounds reasonable to me.

JamesColeman-LW avatar Jun 24 '22 19:06 JamesColeman-LW

Done in cf64ecc96bcb9263f24f61a7c438ecc05eb697de, now in release v1.0.2.

aotimme avatar Dec 30 '22 18:12 aotimme