Skip to content

Instantly share code, notes, and snippets.

@jhyland87
Created December 27, 2017 23:49
Show Gist options
  • Save jhyland87/a6a569b8a3a33fe0477987d7d55fc166 to your computer and use it in GitHub Desktop.
Save jhyland87/a6a569b8a3a33fe0477987d7d55fc166 to your computer and use it in GitHub Desktop.
Awk script to parse output of git and return commands that can be executed to open results in vim Raw
#!/usr/bin/env awk -v cmd=sh -v goto=last -f
# Output of Grep command
# $ grep -RnHsi waldo scripts
# scripts/find-user.js:3:if ( process.argv.length > 1 && process.argv[2] === 'Waldo' )
# scripts/find-user.js:4: console.log("You ARE lookin for Waldo")
# scripts/find-user.js:6: console.log("You are NOT lookin for Waldo")
# scripts/find-user.sh:4:if test -n $1 && [[ $1 == 'Waldo' ]]; then
# scripts/find-user.sh:5: echo "You ARE lookin for Waldo"
# scripts/find-user.sh:7: echo "You are NOT lookin for Waldo"
#
# Same grep command, executing grep2vim using defaults
# $ grep -RnHsi waldo scripts | ./grep2vim.awk
# :e scripts/find-user.sh|:4|:tabe scripts/find-user.js|:3
#
# Output the full command to be executed in a sh/bash terminal
# $ grep -RnHsi waldo scripts | awk -v cmd=sh -f ./grep2vim.awk
# vim -c ":e scripts/find-user.sh|:4|:tabe scripts/find-user.js|:3"
#
# Change the goto lines from the first to the last
# $ grep -RnHsi waldo scripts | awk -v cmd=sh -v goto=last -f ./grep2vim.awk
# vim -c ":e scripts/find-user.sh|:7|:tabe scripts/find-user.js|:6"
#
# Open the files within vim, instead of just spitting out the commands to do so
# $ grep -RnHsi waldo scripts | awk -v cmd=vim -f ./grep2vim.awk | xargs -I {} -o vim -c '{}'
#
# TODO:
# - In the iteration body maybe verifying that NF >= 3 (since anything less would be useless), and skip if not
BEGIN {
FS = ":"
file_lines[""] = "" # List of matching line numbers for each file
file_matches[""] = "" # Keep track of how many times a file was referenced
cmds["vim"] = "" # Array containing the generated commands
}
{
# Increment (or declare) the file_matches entry for this file
file_matches[$1] = ( ( file_matches[$1] == "" ) ?
1 :
file_matches[$1]+1 )
# If this file has NOT been added to file_lines yet, add it and go to next record
if ( file_lines[$1] == "" ){
file_lines[$1] = $2
next
}
# If this file HAS been added to the file_lines array, then only update it if the goto is set to last
if ( goto == "last" ){
file_lines[$1] = $2
next
}
}
END {
delete file_lines[""]
delete file_matches[""]
# Iterate through each file entry, updating final_cmd as needed
for ( lno in file_lines ){
# If this is the first command, then it needs to be different
# if ( length( cmds["vim"] ) == 0 )
# cmds["vim"] = sprintf(":e %s|:%s", lno, file_lines[lno] )
# else
# cmds["vim"] = sprintf("%s|:tabe %s|:%s", cmds["vim"], lno, file_lines[lno] )
cmds["vim"] = ( ( length( cmds["vim"] ) == 0 ) ?
sprintf( ":e %s|:%s", lno, file_lines[lno] ) :
sprintf( "%s|:tabe %s|:%s", cmds["vim"], lno, file_lines[lno] ) )
}
cmds["bash"] = cmds["sh"] = sprintf("vim -c \"%s\"", cmds["vim"])
# If a specific command is requested
if ( cmd != "" ){
if ( cmds[cmd] == "" ){
printf("No command found for %s\n", cmd) > "/dev/stderr"
exit 1
}
print cmds[cmd]
exit 0
}
print cmds["sh"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment