AWK-ing
I needed to do a little slicing of text.
Google Cloud Platform provides billing data as a monthly CSV.
It includes a sku column I needed.
| |
The goal is to get each SKU (column one) and append them all for a SQL IN(...) statement.
So comma separated and quoted like IN("sku1", "sku2",...).
Here’s what I ended up with:
| |
I learned a couple of things doing this so let’s break it down:
- Separator - By default
awkuses a space. The-F','flag defines that separator as a comma instead. - Ignore the first line. The
FNR > 1tellsawkto only process lines with a linenumber greater than 1. I guess this is 1-based rather than zero based. - Single-quote the output. The SQL
INstatement expects strings to be inside single-quotes like'THIS'. There was plenty of advance on Stack Overflow but ultimately I liked the approach of adding quotes toawkoutput using hexadecimal instead of escaping the quotes needed to type a quote as the special character. paste. Never came across this before but it “merges lines of files”. So wherecatandawkemit on line per result,pastewill squish them all into a single line and give me the chance to insert my own separator--delimeteror-d ",".
Output:
| |