# Naming this gts as we primarily want to use this for getting timestamp
# columns from GStreamer logs but is general purpose to be used for other
# things.
function gts --description "Get column next to column matching a pattern" -a file pattern result_file
    if test (count $argv) -lt 3
        echo "Usage: gts <file_name> <search_pattern> <result_file>"
        echo "Eg: gts gstreamer-ts.log pts /tmp/result.csv"
    else
        # Found the trick to use passed argument as an awk pattern below
        # https://stackoverflow.com/questions/39366910/how-to-escape-fish-shell-variable-to-use-as-awk-pattern
        # The actual incantation is a modification from the solution here
        # https://stackoverflow.com/questions/67608136/print-column-next-to-the-column-matching-a-pattern
        if test -e $result_file
            echo "Result file $result_file exists, removing..."
            rm $result_file
        end
        awk -v pat="$pattern" 'BEGIN{FS=OFS=" "} {for(i=1;i<=NF;i++){if($i==pat){print $(i+1);next}}}' $file >>$result_file
        nvim $result_file
    end
end
