Passing a regexp to grep via a shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing a regexp to grep via a shell script
# 1  
Old 11-03-2008
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.
# 2  
Old 11-04-2008
If you can switch to regular expressions (not shell pattern matching),
you could use AWK:
Code:
$ 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.
# 3  
Old 11-05-2008
Thank you very much, Radoulov.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Passing shell script parameter value to awk command in side the script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff |... (1 Reply)
Discussion started by: Sarita Behera
1 Replies

2. Shell Programming and Scripting

To run a local shell script in a remote machine by passing arguments to the local shell script

I need to run a local shell script on a remote machine. I am able to achieve that by executing the command > ssh -qtt user@host < test.sh However, when I try to pass arguments to test.sh it fails. Any pointers would be appreciated. (7 Replies)
Discussion started by: Sree10
7 Replies

3. Shell Programming and Scripting

A help in regexp and grep

I have test string value , something like the one below str='KUAMRJIT|GHOSH' If I type echo $str | grep -o -e '\|+' it doesnt give me anything . But on the contrary echo $str | grep -o -e '|' display the only one pipe character(|) thats there in the string above . The way I understood Unix... (8 Replies)
Discussion started by: kumarjt
8 Replies

4. UNIX for Dummies Questions & Answers

Grep Regexp not working correctly

Consider the following code: grep -o -e '^STEAM_::\d+$' workfile3.tmp A sample format of a valid string for the regexp would be: STEAM_0:1:12345678 Here is an example line from the workfile3.tmp file: 465:L 01/02/2012 - 00:05:33: "Spartan1-1-7<8><STEAM_0:1:47539638><>" connected No... (2 Replies)
Discussion started by: spinner0205
2 Replies

5. Shell Programming and Scripting

Issue with passing variable to Grep in a shell script

Hi, I'm trying to check if methods specified in a class have been added to the corrosponding interface. My code below is giving me the following errors: grep: function: No such file or directory grep: import($zipfile): No such file or directory grep: function: No such file or... (1 Reply)
Discussion started by: racshot65
1 Replies

6. Shell Programming and Scripting

help with grep regexp

My input file looks like this: 13154|X,the deer hunter 13154|Y,the good life 1316|,american idol 1316|,bowling 1316|,chuck etc... The X, Y, or any other character (besides a comma) after the pipe is a "Device Type". I want to strip out lines that do not have a device type. I have... (2 Replies)
Discussion started by: jwinsk
2 Replies

7. Shell Programming and Scripting

Passing variable from shell script to python script

I have a shell script main.sh which inturn call the python script ofdm.py, I want to pass two variables from shell script to python script for its execution. How do i achieve this ????? Eg: main.sh a=3 b=3; c= a+b exec python ofdm.py ofdm.py d=c+a Thanks in Anticipation (4 Replies)
Discussion started by: shashi792
4 Replies

8. Shell Programming and Scripting

Passing parameters to Shell script for GREP command

I am using grep to capture date from a file . Since i need to use the shell script for different dates ,is it possible to pass the date parameter to the shell script the Script is as below grep -E "08 Aug 2008|2008-08-08"* somefile.txt>test.txt The above script file greps the... (1 Reply)
Discussion started by: sud.tech
1 Replies

9. Shell Programming and Scripting

passing parameter from Shell-script to Sql-script

Dear Friends, Please help me to achieve the following: I want to pass one parameter from Shell-script to Sql-script. Example: My ShellScript.sh is calling report.sql like this: /bin/sqlplus /reports.sql And My report.sql is calling many Stored-Procedures like this: exec... (0 Replies)
Discussion started by: subodhbansal
0 Replies

10. UNIX for Dummies Questions & Answers

grep using regexp

I have 2 files called stuff-egress-filter and stuff-ingress filter. There are also files called something like stuff-egress-F/0 I want to match the first two... I tried (i realize there is no filename... I'm piping this from the ls command) grep stuff-*-filter Finds nothing. If I... (18 Replies)
Discussion started by: earnstaf
18 Replies
Login or Register to Ask a Question