Erudes
Erudes

Reputation: 25

Combining grep and find for desired results

I am trying to list files found from a script as "File Size - Path". What my script needs to filter is whenever I give it TWO parameters and the first one is "-u", then the files I'll be looking for are the ones that contain the word "Priority" at the beginning of the file. (The second parameter is always a directory).

So far I have this:

    if [ "$1" = -u ]
    for i in `grep -ril ^Priority "$2"`
    do
    echo | ls -lh `grep -ril ^Priority "$i"` | cut -d" " -f5,9
    done
    fi

It returns that the end of the file was not expected on line 7.

However when I run this:

for i in `grep -ril ^Prioridad "$1"`
do
echo | ls -lh `grep -ril ^Prioridad "$i"` | cut -d" " -f5,9
done

Returns desired results.

How can I get the first one working with such results but using -u as first parameter and directory as second one?

Upvotes: 0

Views: 199

Answers (1)

asio_guy
asio_guy

Reputation: 3767

You missed then:

if [ "$1" = -u ]; then # <-- here
    for i in `grep -ril ^Priority "$2"`
    do
      # Some more edits and trying to remove the echo/ls
      # and battling against not found scenario
       if grep -ril ^Priority "$i" ; then # if exists 
          echo "$(stat -c%s $i) $i" # stat size and File
       fi
    done
fi

Upvotes: 3

Related Questions