Hacker News new | past | comments | ask | show | jobs | submit login

Looks a lot more readable than the UNIXy alternatives below



yeah. And unlike many of the Unixy solutions it will actually produce correct results even if the input data contained quotes ("), apostrophes ('), commas (,), backslashes (\) and other characters that could throw parsing and text synthesizing off.

* The iwr (Invoke-WebRequest) reads the text file from the uri.

* The ConvertFrom-Json cmdlet reads the content as json and parses it according to json rules. The output is a sequence of objects (not json objects, but PowerShell objects) with properties corresponding to the json fields. This will respect specially encoded characters.

* The ? (select-object) cmdlet can filter on complex expressions, but in this case it just filters on the presence of a creditcard property.

* The select (Select-Object) cmdlet selects just the properties we want to export, out of all of the properties for each object.

The epcsv (Export-Csv) exports objects from the pipeline and outputs them to a file with proper escaping for e.g. quotes, apostrophes etc.

By not relying on home-cooked text munging you can actually produce a robust solution that is also readable.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: