counting the numbers in a row

File A

aa <space>  --D--A--D---DDY---M--UM-M--MY

Another file

D3
M9
 So output shud be 
Here in FileA   D which is 3 after removing dash 
 after we have counted  dash   D is position at 9
and for M is  23 

final output will be 
D9
M23

have you thought of a plan to solve this?

Something like this,

#!/bin/sh

while read line
do
ch=`echo $line | cut -c1`
number=`echo $line | cut -c2-`
awk '{print $2}' file1 | awk -v v1=$ch -v v2=$number -F"[A-Z]" '{for (i=1;i<=NF;i++) { if (i == v2 ) {c += length($i) + v2 ;print v1 c;exit }c += length($i) }}'
done < file2
cat file1
aa     --D--A--D---DDY---M--UM-M--MY

cat file2
D3
M9

sh test77.sh
D9
M23
1 Like