Use git grep -i <searchterm> but exclude files with long lines (e.g., lines longer than 300 characters)
The proposed answer was:
bash
git ls-files | while read file; do
if ! awk 'length > 300 { exit 1 }' "$file"; then
continue
fi
git grep -i -- "$searchterm" "$file"
done
This logic has a bug: it skips files that do have long lines, but the condition is reversed. It uses:
bash
if ! awk 'length > 300 { exit 1 }' "$file"; then
Which means: "If any line is longer than 300 → exit 1 (i.e., failure) → ! makes it true → we skip (continue)"
So it's skipping files with long lines — which is the opposite of what we want.
✅ Corrected Version
We want to skip files that contain any line longer than 300 characters, so we should only run git grep on files that don’t have such long lines.
Here's the corrected version:
bash
git ls-files | while read -r file; do
if awk 'length > 300 { exit 1 }' "$file"; then
git grep -i -- "$searchterm" "$file"
fi
done
🔍 Explanation:
git ls-files: Lists all files tracked by Git.
while read -r file: Safely reads each filename into the file variable.
awk 'length > 300 { exit 1 }' "$file":
If any line in $file is longer than 300 characters, awk exits with code 1.
So this returns false for files with long lines → they are skipped.
if ... then git grep ...: Only runs git grep on files without long lines.
🛠 Optional Enhancement
To make it more efficient and readable, you could also write it as:
bash
git ls-files | while read -r file; do
# Skip files with lines longer than 300 chars
if awk '{ if (length > 300) { exit 1 } }' "$file"; then
git grep -i -- "$searchterm" "$file"
fi
done
Or even use a helper function:
bash
has_long_lines() {
awk '{ if (length > 300) exit 1 }' "$1"
}
git ls-files | while read -r file; do
if has_long_lines "$file"; then
git grep -i -- "$searchterm" "$file"
fi
done
Let me know if you want a version that also excludes binary files or works recursively in subdirectories!
…to search for a term case-insensitively with line numbers, and only show matches in files that have no line longer than 300 characters.
Now you want the output to be colored, like how git grep normally colors its output.
🔍 Problem
The issue is that when you pipe git grep into another command (like awk), it disables color output by default. So you need to force Git to emit color codes, and make sure they survive through the pipe.
Search all tracked files for "error", case-insensitive.
Show line numbers.
Only show matches from files where no line exceeds 300 characters.
Keep the output colored as in normal git grep.
Let me know if you'd like to highlight specific parts (e.g., just the match, not the whole line)!
User
awk processes the colored output without stripping the ANSI color codes, i think sed does, how do other unix cmdline utilities behave
Assistant
You're absolutely right to be concerned about how Unix command-line utilities handle ANSI color codes (like those used for terminal coloring). Let's break this down clearly.
✅ Summary Up Front:
awk: Does not strip ANSI escape codes, but you must be careful with how you process the input.
sed: Also does not strip ANSI codes, and can even modify them if needed.
Most Unix tools (grep, cut, sort, etc.) do not inherently strip or interpret ANSI codes, they just treat them as text — unless they’re specifically designed to do so.
But there are important nuances depending on:
Whether the tool is aware of terminal formatting
How it processes lines internally
Whether it’s part of a pipeline
🔍 Behavior by Tool
Tool
Handles ANSI?
Notes
awk
✅ Yes, preserves ANSI codes
Just treats them as characters. Use length($0) carefully since color codes add invisible length.
sed
✅ Yes, preserves ANSI codes
Can also manipulate or remove them using regex.
grep
✅ Preserves ANSI if given via --color=always
Output will retain colors when piped if told to.
cat
✅ Passes ANSI through
No effect on color codes.
cut
✅ Preserves ANSI
But may cut in the middle of escape sequences. Not recommended for colored output.
sort
✅ Preserves ANSI
Sorts based on raw byte values; color codes affect sort order unintentionally.