fish: functions/gts: Add a helper to get column of interest from log files

This commit is contained in:
Sanchayan Maity 2022-04-16 13:34:41 +05:30
parent c936941188
commit 800ec4ca64

View file

@ -0,0 +1,20 @@
# 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