Problem with special characters....

grep -i "$line,$opline" COMBO_JUNK|awk -F, '
  {
    C4+=$4
  }
  {
  }
  END {
    print C4
  }
' OFS=,`

[/CODE]

when i run this command in the script.... it o/p all the value as 0 if $line contains any special parameters.....

but the same script if i run in command prompt... it shows proper o/p

grep -i "ADESH,id2_1_\*,airtel" COMBO_JUNK|awk -F, '
  {
    C4+=$4
  }
  {
  }
  END {
    print C4
  }
' OFS=,

[/CODE]

here $line is ADESH,id2_1_\*

[/CODE]

i have escape the meaninf of * by giving \.

---------- Post updated at 09:52 AM ---------- Previous update was at 09:49 AM ----------

while read line
do
while read opline
do
var=`grep -i "$line,$opline" COMBO_JUNK|awk -F, '
  {
    C4+=$4
  }
  {
  }
  END {
    print C4
  }
' OFS=,`
if [ "$var" ]
then
echo ",$var" >> hope_$opline
else
var=0
echo ",$var" >> hope_$opline
fi
done<operator.txt
done<pcstates.txt

[/CODE]

pcstates.txt

ADESH,id1_0
ADESH,id2_1_\*
ADESH,id2_2
ADESH,id2_3
ADESH,id2_5
ADESH,id2_7
ADESH,id3_2_\*
ADESH,id3_3
ADESH,id4_2
ADESH,id4_3
ADESH,id4_4
ADESH,id4_5
ADESH,id4_6
ADESH,id4_7
ADESH,id4_8
ADESH,id4_9
ADESH,id4_10
ADESH,id5_1_\*

[/CODE]

operator.txt

AIL
AIEL
DOMO
VONE

[/CODE]

The escaping may be too many. Pls post an execution log (reduced to the meaningful lines) using the -x option in the script.

let me make the question simpler...

if i execute below command in prompt then it executes fine...
i get the o/p

grep -i "$line,$opline" COMBO_JUNK|awk -F, '
  {
    C4+=$4
  }
  {
  }
  END {
    print C4
  }
' OFS=,

[/CODE]
but when i run it in the script...
it is not substituting $line,$opline properly

Execution logs

++ grep -i AADESH,id1_0,AELA COMBO_JUNK
++ awk -F, '
  {
    C4+=$4
  }
  {
  }
  END {
    print C4
  }
' OFS=,
+ var=43
+ '[' 43 ']'
+ echo ,43
+ read opline
++ grep -i AADESH,id1_0,AEL COMBO_JUNK
++ awk -F, '
  {
    C4+=$4
  }
  {
  }
  END {
    print C4
  }
' OFS=,
+ var=1790
+ '[' 1790 ']'
+ echo ,1790
+ read opline
++ grep -i AADESH,id1_0,DOMO COMBO_JUNK
++ awk -F, '
  {
    C4+=$4
  }
  {
  }
  END {
    print C4
  }
' OFS=,
+ var=82
+ '[' 82 ']'
+ echo ,82
+ read opline
++ grep -i AADESH,id1_0,VONE COMBO_JUNK
++ awk -F, '
  {
    C4+=$4
  }
  {
  }
  END {
    print C4
  }
' OFS=,
+ var=309
+ '[' 309 ']'
+ echo ,309
+ read opline
+ read line
+ read opline
++ grep -i 'AADESH,id2_1_*,AELA' COMBO_JUNK
++ awk -F, '
  {
    C4+=$4
  }
  {
  }
  END {
    print C4
  }
' OFS=,
+ var=
+ '[' '' ']'
+ var=0
+ echo ,0
+ read opline
++ grep -i 'AADESH,id2_1_*,AEL' COMBO_JUNK
++ awk -F, '
  {
    C4+=$4
  }
  {
  }
  END {
    print C4
  }
' OFS=,
+ var=
+ '[' '' ']'
+ var=0
+ echo ,0
+ read opline
++ grep -i 'AADESH,id2_1_*,DOMO' COMBO_JUNK
++ awk -F, '
  {
    C4+=$4
  }
  {
  }
  END {
    print C4
  }
' OFS=,
+ var=
+ '[' '' ']'
+ var=0
+ echo ,0
+ read opline
++ grep -i 'AADESH,id2_1_*,VONE' COMBO_JUNK
++ awk -F, '
  {
    C4+=$4
  }
  {
  }
  END {
    print C4
  }
' OFS=,
+ var=
+ '[' '' ']'
+ var=0
+ echo ,0
+ read opline
+ read line
+ read opline
++ grep -i AADESH,id2_2,AELA COMBO_JUNK
++ awk -F, '

[/CODE]

Et voila - there you are:

It is looking for strings with 0 to n occurrences of _ , not \* , which was what you intended. So - it's not an escape too many, but one too few.

btw - when mentioning "meaningful lines" I was talking of those around the occurrence of the problem.
btw2 - indenting and pretty printing like you do in your awk program usually is good practice, but in this simple case a one liner might be easier to read and handle: awk -F, '{C4+=$4} END {print C4}' will do!

thanks RUDI... i fixed it....

i used

id2_1_\\\* instead of id2_1_\*

thanks for ur reply... :slight_smile: