Passing a regexp to grep via a shell script

Hello,

I have the output of ls -l stored in a text file called "files.txt".

-rwx------ 1 user1 dev 130 Sep 21 16:14 sc1.sh
-rwxr----- 1 user1 dev 10328 Sep 29 20:11 sc10.sh
-rwxr----- 1 user1 dev 9984 Sep 30 15:33 sc11.sh
-rwxr----- 1 user1 dev 9987 Oct 1 10:05 sc12.sh
-rwx------ 1 user1 dev 3215 Sep 21 17:15 sc2.sh
-rwx------ 1 user1 dev 3215 Sep 22 10:04 sc3.sh
-rwxr----- 1 user1 dev 2174 Sep 22 13:34 sc4.sh
-rwxr----- 1 user1 dev 2837 Sep 22 16:35 sc5.sh
-rwxr----- 1 user1 dev 5923 Sep 23 11:51 sc6.sh
-rwxr----- 1 user1 dev 5995 Sep 23 13:20 sc7.sh
-rwxr----- 1 user1 dev 6458 Sep 24 13:37 sc8.sh
-rwxr----- 1 user1 dev 8375 Sep 26 10:53 sc9.sh

I need to extract the filename and filesize, viz column 9 & 5 for all files matching a filename pattern into another file.

I have tried the following shell script.

set -f
FILE_EXT=*.sh
awk '{print $9" "$5}' files.txt | grep "$FILE_EXT"

It does not print the rows with the patterns. The pattern used for grep through the shell script needs to be configurable via the command line.

Can anybody help me with this.

Thanks.

If you can switch to regular expressions (not shell pattern matching),
you could use AWK:

$ FILE_EXT='\\.sh$'                                                 
$ awk '$0 ~ pattern { print $9, $5 }' pattern="$FILE_EXT" files.txt 
sc1.sh 130
sc10.sh 10328
sc11.sh 9984
sc12.sh 9987
sc2.sh 3215
sc3.sh 3215
sc4.sh 2174
sc5.sh 2837
sc6.sh 5923
sc7.sh 5995
sc8.sh 6458
sc9.sh 8375

You should escape twice because the interpreter should scan the dynamic regular expression twice.

Thank you very much, Radoulov.