Help in script

Hi,

I have the below script:

 
/opt/app/dstage/DataStage752/Ascential/DataStage/DSEngine/bin/dsjob lprojects >> prj
j=`cat prj|wc -l|sed -e 's/^[ \t]*//'` 
for i in {1..$j}
do
pr=head -$i prj
k=`cat status.txt| grep $pr |wc -l|sed -e 's/^[ \t]*//'` 
echo "there are $k  in $pr" >> body.txt
done

The first line in the script will create a file called 'prj' which contains the belo data:

abc
bcd
cde

So, the j value becomes 3.
so the for loop will execute from 1 to 3.

The file status.txt contains the below data:

1-abc
11-abc
12-abc
1-bcd
0-bcd
1-cde
12-cde
123-cde

So,in the first iteration $pr becomes abc.
While grepping for abc in status.txt there are 3. so $k becomes 3.
So body.txt will get the content as "there are 3 in abc".

In the next iteration, $k becomes 2 (since there are 2 bcd in status.txt).
So the line "there are 2 in bcd" will be appended to body.txt.

But it's not working as expected.

Can any one tell me where i am making mistakes in the script?

Thanks

---------- Post updated at 12:38 PM ---------- Previous update was at 11:25 AM ----------

Silly mistake:

head -1 will work correctly

but while going with head -2 , it causes the failure in grep

---------- Post updated at 12:42 PM ---------- Previous update was at 12:38 PM ----------

In stead of head , i would like to go with sed -n option.

BUt i am getting syntax error for the below code:

 
pr=`sed -n '$i{p}' prj|sed -e 's/^[ \t]*//'`

can any one help me?

Thanks

embedding a shell variable in sed:

sed -n ''"$i"'p' datafile
 
or
 
sed -n "${i}p" datafile
 

You can replace that pipeline with a simple wc -l < prj . When wc reads from standard input instead of a named file command line argument, it does not output any name.

Should you care for it, here's a simpler, more efficient approach:

awk -F- '
    NR==FNR {k[$0]=0; next}
    $2 in k {k[$2]++}
    END {for (i in k) print "There are " k " in " i}
' prj status.txt > body.txt

Regards,
Alister