#!/bin/bash
# Script to convert git diff output to a searchable format# Check if a git repository if [ ! -d .git ]; then echo "This directory is not a git repository." exit 1 fi
# Filename for the output output_file="git_diff_searchable.txt"
# Empty the output file or create it if it doesn't exist > "$output_file"
# Process git diff output git diff --unified=0 | while read line; do # Check for filename line if [[ $line =~ ^diff ]]; then filename=$(echo $line | sed 's/diff --git a\/\(.\) b\/./\1/') elif [[ $line =~ ^@@ ]]; then # Extract line numbers line_numbers=$(echo $line | sed -E 's/@@ -[0-9]+(,[0-9]+)? \+([0-9]+)(,[0-9]+)? @@./\2/') else # Write filename and line number to the output file echo "$filename:$line_numbers: $line" >> "$output_file" fi done
echo "Output saved to $output_file"
I then ran the following egrep [corrected to egrep, after mistakenly putting that I used gawk] command egrep -e 'agent.rs:[0-9]{1,}' git_diff_searchable.txt* to see the results. Everything worked as I expected.
Now, I don't claim that this is what you intended to achieve but I prompted it with the context of what you asked: Write a script that converts git-diff output to a file that can be easily grepped by filename and linenumber.