How to use grep command in shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use grep command in shell script?
# 1  
Old 10-04-2010
How to use grep command in shell script?

Hi,

I am new to shell scripting.
I had written a small code which accepts config file as parameter. In config file I mentioned a path for input files,
Code:
 
INPUT_FILE_FOLDER=/project/dev/con/InputFile
SEARCH_MASK_INPUT_FILE=ABC_CR_PQR*.*

I want to use this path to get all file names from folder.
Code:
 
INPUT_LIST_FILES=`ls $INPUT_FILE_FOLDER | grep -i $SEARCH_MASK_INPUT_FILE`
 for file in $INPUT_LIST_FILES
  do
     echo $file
  done

but i am getting below message as,
Code:
 
Usage: grep [-E|-F] [-c|-l|-q] [-insvxbhwy] [-p[parasep]] -e pattern_list...
        [-f pattern_file...] [file...]

Could you please help me out.
# 2  
Old 10-04-2010
try make use of egrep
Code:
egrep -i "$SEARCH_MASK_INPUT_FILE"

# 3  
Old 10-04-2010
I used egerp, but still I am getting same message.
# 4  
Old 10-04-2010
Try with double quote over search_mask_input_file, it'll work
Code:
INPUT_LIST_FILES=`ls $INPUT_FILE_FOLDER | egrep -i "$SEARCH_MASK_INPUT_FILE" `
 for file in $INPUT_LIST_FILES
  do
     echo $file
  done

# 5  
Old 10-04-2010
The pattern is a globbing pattern, not a regex. Try this:
Code:
for file in "$INPUT_FILE_FOLDER/"$SEARCH_MASK_INPUT_FILE; do
  echo $file
done

# 6  
Old 10-04-2010
I am executing script from another location and my input files are in different location.

So given solution is not working.
If I write "$SEARCH_MASK_INPUT_FILE", then I am getting list of all files and folders present in the current location.
This User Gave Thanks to Poonamol For This Post:
# 7  
Old 10-04-2010
Do you mean on a remote system or in a different directory. If the latter is the case then my suggestion should work.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell 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 | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

2. Shell Programming and Scripting

Shell Script to grep output from top command.

Hello, I want a script which would grep details from top command for specific processes. I m not sure of the PID of these processes but i know the names. $ top -c top - 16:41:55 up 160 days, 5:53, 2 users, load average: 9.36, 9.18, 8.98 Tasks: 288 total, 9 running, 279 sleeping, 0... (8 Replies)
Discussion started by: Siddheshk
8 Replies

3. Shell Programming and Scripting

performance of shell script ( grep command)

Hi, I have to find out the run time for 40-45 different componets. These components writes in to a genreric log file in a single directory. eg. directory is LOG and the log file name format is generic_log_<process_id>_<date YY_MM_DD_HH_MM_SS>.log i am taking the run time using the time... (3 Replies)
Discussion started by: vikash_k
3 Replies

4. Shell Programming and Scripting

can anyone help with shell script command about searching word with grep command?

i want to search in the current directory all the files that contain one word for example "hello" i want to achieve it with the grep command but not with the grep * (2 Replies)
Discussion started by: aintour
2 Replies

5. Shell Programming and Scripting

Grep command in shell script

Hi, I have written the following shell script - Error_String="error" var1="| grep -v note1 | grep -v note2" grep -i $Error_String /users/mqm/Pwork/Err/*.out $var1 The above script gives error saying "grep: can't open | grep: can't open grep grep: can't open -v" etc In my program... (3 Replies)
Discussion started by: prasannasupp
3 Replies

6. 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

7. Shell Programming and Scripting

Grep command usage in shell

hi, I am facing a problem when i use grep command in shell script. problem is as follows... 1)i has declared an variable (say a) 2)Now i am searching for an pattern in a file and i used the followig command cat /wls_../../scripts/log.txt | grep $a-JUL-08 i am not getting any... (6 Replies)
Discussion started by: hemanth_t
6 Replies

8. Shell Programming and Scripting

how to redirect the output of a grep command to a file inside a shell script

hi, i wat to get the output of a grep command in a file. but when i am trying out the same grep command in the unix prompt its working fine.. i am getting the output properly.. but when i am writing the same command inside my shell script , its just creating a new output file with no contents... (11 Replies)
Discussion started by: kripssmart
11 Replies

9. UNIX for Dummies Questions & Answers

how to get the output of a grep command to a file inside a shell script

hi, i wat to get the output of a grep command in a file. but when i am trying out the same grep command in the unix prompt its working fine.. i am getting the output properly.. but when i am writing the same command inside my shell script , its just creating a new output file with no contents... (1 Reply)
Discussion started by: kripssmart
1 Replies

10. UNIX for Dummies Questions & Answers

grep command & shell procedure's

hello, i'm trying to pluck words from a list that have exactly 3 occurances of a specified letter. I've come up with this : grep -i .*$1.*$1.*$1.* But this also selects words with 4 or more, any tips? I'm putting this into a shell procedure and want to be able to add a switch before I put the... (1 Reply)
Discussion started by: stevox
1 Replies
Login or Register to Ask a Question