Skip to content

Instantly share code, notes, and snippets.

@zacharysyoung
Last active February 9, 2022 18:53
Show Gist options
  • Save zacharysyoung/c0b222b586945e6e4e8ca9cb2eca1d8d to your computer and use it in GitHub Desktop.
Save zacharysyoung/c0b222b586945e6e4e8ca9cb2eca1d8d to your computer and use it in GitHub Desktop.
#!/bin/sh
# Join Part-A and Part-B
gocsv join -c 'label' -outer file1.csv file2.csv > joined.csv
echo 'Joined'
gocsv view joined.csv
# Rename the two samely-named 'label' columns to unique names
gocsv rename -c 1 -names 'Label_A' joined.csv | gocsv rename -c 3 -names 'Label_B' > renamed.csv
echo 'Renamed key cols'
gocsv view renamed.csv
# Add 'label' back in, as a composite of the two;
# -t ("template") turns Label_A & Label_B into a list, removes empty (compact), and takes the first value remaining
gocsv add -name 'label' -t '{{ list .Label_A .Label_B | compact | first }}' renamed.csv > added.csv
echo 'Added "composite" label'
gocsv view added.csv
# Pare-down to desired columns
gocsv select -c 'label','Part-A','Part-B' added.csv > pared_down.csv
echo 'Pared down'
gocsv view pared_down.csv
# Add 'NA' and sort
gocsv replace -c 'Part-A','Part-B' -regex '^$' -repl 'NA' pared_down.csv | gocsv sort -c 'label' > final_columns.csv
echo 'Final'
gocsv view final_columns.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment