gocsv
gocsv copied to clipboard
Output column raw value
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, 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?
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'
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:
- The
--raw-output
/-r
would be an option only for theselect
subcommand. - 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. - 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!
That sounds reasonable to me.
Done in cf64ecc96bcb9263f24f61a7c438ecc05eb697de, now in release v1.0.2.