Spartacus Spartacus -4 years ago 137
Bash Question

Grep command giving unexpected output when searching exact word in file in csh

I used following script to search every line of one file in another file and if it is found printing 2nd column of that line :


set goldFile=$1
set regFile=$2

set noglob
foreach line ("`cat $goldFile`")
set searchString=`echo $line | awk '{print $1}'`
set id=`grep -w -F "$searchString" $regFile | awk '{print $2}'`
echo "$searchString" "and" "$id"

unset noglob

Gold file is as follows :


and RegFile is as follows :

\$#%$%escaped.Integer%^^&[10] 1
\$#%$%escaped.Integer%^^&[10][0][0][31] 10
\$#%$%escaped.Integer%^^&[10][0][0][30] 11
\$#%$%escaped.Integer%^^&[10][0][0][29] 12
\$#%$%escaped.Integer%^^&[10][0][0][28] 13
\$#%$%escaped.Integer%^^&[10][0][0][27] 14
\$#%$%escaped.Integer%^^&[10][0][0][26] 15

Output is coming :

\$#%$%escaped.Integer%^^&[10] and 1 10 11 12 13 14 15
\$#%$%escaped.Integer%^^&[10][0][0][31] and 10
\$#%$%escaped.Integer%^^&[10][0][0][30] and 11
\$#%$%escaped.Integer%^^&[10][0][0][29] and 12
\$#%$%escaped.Integer%^^&[10][0][0][28] and 13
\$#%$%escaped.Integer%^^&[10][0][0][27] and 14
\$#%$%escaped.Integer%^^&[10][0][0][26] and 15

But expected Output is :

\$#%$%escaped.Integer%^^&[10] and 1
\$#%$%escaped.Integer%^^&[10][0][0][31] and 10
\$#%$%escaped.Integer%^^&[10][0][0][30] and 11
\$#%$%escaped.Integer%^^&[10][0][0][29] and 12
\$#%$%escaped.Integer%^^&[10][0][0][28] and 13
\$#%$%escaped.Integer%^^&[10][0][0][27] and 14
\$#%$%escaped.Integer%^^&[10][0][0][26] and 15

Please help me to figure out how to search exact word having some special character using grep.
Thanks in advance.

Answer Source

csh and bash are completely different variants of shell. They're not even supposed to be compatible. Your problem is more associated with usage of grep

Because of the -F flag in grep which lets your string to be fixed pattern, prone to contain all sorts of regex special characters like ,,[],(),.,*,^,$,-,\

The error result is because the -F flag, the line \$#%$%escaped.Integer%^^&[10] in Gold file matches all the input lines on the RegFile.

So normally the exact words of search can be filtered by the word boundary constructs ^ and $ as part of the pattern, but it won't work in your case because of the -F, --fixed-strings flag they will be treated as being part of the search string.

So assuming from the input file, there could be only one match for each line in the Gold file to RegFile you could stop the grep search after the first hit

Using the -m1 flag, which according to the man grep page says,

-m NUM, --max-count=NUM
          Stop reading a file after NUM matching lines.  If the input is standard input 
          from a regular file, and NUM matching lines are output, grep ensures that the 
          standard input  is  positioned  to just  after  the  last  matching  line  before  
          exiting,  regardless of the presence of trailing context lines.  

So adding it like,

grep -w -F -m1 "$searchString" $regFile

should solve your problem.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download