Skip to content

Instantly share code, notes, and snippets.

@ErgEnn
Last active September 12, 2023 12:42
Show Gist options
  • Save ErgEnn/498e44e702484cfa4bb704a2efb34f57 to your computer and use it in GitHub Desktop.
Save ErgEnn/498e44e702484cfa4bb704a2efb34f57 to your computer and use it in GitHub Desktop.
yq cheatsheet for csv

Open query output in vscode

yq -p=csv -o=csv sample.csv | code -

Example csv

foo,bar,baz
abc,1,-1
def,2,-2
ghi,3,-3

Get only rows where true

yq -p=csv -o=csv 'filter(.bar>2)' sample.csv

outputs

foo,bar,baz
def,2,-2
ghi,3,-3

Get only rows where true

yq -p=csv -o=csv 'filter(.bar==3 or .baz==-1)' sample.csv

outputs

foo,bar,baz
abc,1,-1
ghi,3,-3

Get number of results

yq -p=csv -o=csv 'filter(.bar>2)|length' sample.csv

outputs

2

Using quotes in PowerShell

Escape like """foo""" or \"foo\"

Filter out specific items and create new objects for each(note: for some reason having multiple fields in new object slows down the operation dramatically)

yq -p=csv -o=csv '[filter(.bar>2)|.[]|{\"barbaz\": .bar+.baz}]' sample.csv

or

yq -p=csv -o=csv '[.[]|select(.bar>2)|{\"barbaz\": .bar+.baz}]' sample.csv

outputs

barbaz
0
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment