|
| From: | Heime |
| Subject: | Storing grep results in variable |
| Date: | Fri, 06 Jun 2025 06:06:05 +0000 |
Have made this function. I wonder whether variable "matches" will get too
large.
Can I improve this?
mysearch ()
{
local search_dir=$1
local search_str=$2
if [[ -z "$search_dir" || -z "$search_str" ]]; then
echo "Usage: mysearch <directory> <search_string>"
return 1
fi
find "$search_dir" -type f | while IFS= read -r file; do
matches=$(grep -n -- "$search_str" "$file" | sed 's/:/: /1' )
if [[ -n "$matches" ]]; then
echo "$file"
echo "$matches"
echo
fi
done
}
| [Prev in Thread] | Current Thread | [Next in Thread] |