help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Storing grep results in variable


From: microsuxx
Subject: Re: Storing grep results in variable
Date: Fri, 6 Jun 2025 14:55:57 +0200

limit is system ram

u can also store the results in an array
mapfile -t matches < <( grep .. )

may result in more mem needed
but can go with bash code thru em

storing the results in a file is around the limit and slowness

grep .. | tee outfile

On Fri, Jun 6, 2025, 2:42 PM Heime via <help-bash@gnu.org> wrote:

> 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
>  }
>
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]