Unix/Linux Go Back    

Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Grep command is not search the complete pattern

Shell Programming and Scripting

Thread Tools Search this Thread Display Modes
Old Unix and Linux 06-25-2013   -   Original Discussion by sumit.vedi1988
sumit.vedi1988 sumit.vedi1988 is offline
Registered User
Join Date: May 2012
Last Activity: 25 June 2013, 6:28 AM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Grep command is not search the complete pattern

I am facing a problem while using the grep command in shell script. Actually I have one file (PCF_STARHUB_20130625_1) which contain below records.


And I have a pattern which is stored in one variable (INPUT_FILE_T), and want to search the pattern from the file (PCF_STARHUB_20130625_1). For that I have used below command

grep -h ${INPUT_FILE_T} PCF_STARHUB_20130625_1

The output of above command is coming as below


Problem is that only one entry is showing in output (It should contain two entries) output should come like below


Is there any technique except grep please tell me.
Please help me on this issue.

Last edited by sumit.vedi1988; 06-25-2013 at 07:11 AM.. Reason: formating
Sponsored Links
Old Unix and Linux 06-25-2013   -   Original Discussion by sumit.vedi1988
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Join Date: Jul 2012
Last Activity: 18 November 2017, 7:45 PM EST
Location: San Jose, CA, USA
Posts: 10,652
Thanks: 571
Thanked 3,730 Times in 3,184 Posts
The grep utility evaluates basic regular expressions. Unfortunately, (SH?*???????????????US.*) is a filename matching pattern; not a BRE.

To search for lines in a file that match a pattern matching expression, try the following shell script using any shell that recognizes basic Bourne shell syntax (such as ksh and bash):

while IFS='' read -r f
do      case "$f" in
        ($INPUT_FILE_T) printf "%s\n" "$f";;
done < PCF_STARHUB_20130625_1

Furthermore, since you didn't quote the expansion of $INPUT_FILE_T in your grep command, the shell expanded that variable into a list of matching filenames in the current directory before calling grep; so (assuming that the file PCF_STARHUB_20130625_1 contained a list of some of the files in the current directory) the command that you ran was expanded by the shell to:

grep -h SH_5.55916.00.00.100029_20130601_0001_US.csv.gz|349|1700116234 SH_5.55916.00.00.100038_20130601_0001_US.csv.gz|199|2099616349 PCF_STARHUB_20130625_1

which treated SH_5.55916.00.00.100029_20130601_0001_US.csv.gz|349|1700116234 as a basic regular expression that happens to match itself when looking in the file PCF_STARHUB_20130625_1 and, fortunately, doesn't seem to have matched any lines in the file named SH_5.55916.00.00.100038_20130601_0001_US.csv.gz|199|2099616349.

To use grep instead of a loop in the shell, you could translate the filename matching pattern SH?*???????????????US.* to a corresponding BRE (SH..*...............US[.].* or more succinctly SH.\{16,\}US[.].*) and use:

grep "$INPUT_FILE_T_BRE" PCF_STARHUB_20130625_1

Note that the double quotes in the above grep command are crucial to keep the shell from trying to expand the BRE as a filename matching pattern
Sponsored Links
Old Unix and Linux 06-25-2013   -   Original Discussion by sumit.vedi1988
rbatte1 rbatte1 is offline Forum Staff  
Root armed
Join Date: Jun 2007
Last Activity: 17 November 2017, 12:53 PM EST
Location: Lancashire, UK
Posts: 3,365
Thanks: 1,453
Thanked 665 Times in 598 Posts
It could be as simple as quoting your search string:-
grep -h "${INPUT_FILE_T}" PCF_STARHUB_20130625_1

It's an odd search string though. From the man page I have on RHEL 6.1, I have:-

A regular expression may be followed by one of several repetition operators:
      ?      The preceding item is optional and matched at most once.
      *      The preceding item will be matched zero or more times.
      +      The preceding item will be matched one or more times.
      {n}    The preceding item is matched exactly n times.
      {n,}   The preceding item is matched n or more times.
      {,m}   The preceding item is matched at most m times.
      {n,m}  The preceding item is matched at least n times, but not more than m times.

So that you mean that you are looking for a record that starts (doesn't have to be at the beginning of the line) with an S, then the H is optional and then I get confused.

Are you trying to use the ? as a single character each time?

I would think a better search string would be more like:-

to represent Start of line, SH, then any 3 characters, then US. The remainder of the line can be ignored.

Do either of these meet your needs?


Last edited by rbatte1; 06-25-2013 at 10:59 AM.. Reason: Grammar
Sponsored Links

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Pattern search using grep command ! nikesh29 Shell Programming and Scripting 10 03-08-2013 05:01 AM
pipe search pattern into a grep guessingo Shell Programming and Scripting 4 05-04-2012 11:20 AM
search for a pattern using grep manasa_vs Shell Programming and Scripting 7 01-17-2012 07:53 AM
Can grep command return word instead of complete line dashing201 UNIX for Dummies Questions & Answers 11 12-28-2010 05:58 AM
search pattern by grep vijaysabari Shell Programming and Scripting 3 06-23-2004 04:03 AM

All times are GMT -4. The time now is 08:49 PM.