complinagetest () #function name
{
if [ -f complins.dat ];then
rm complins.dat
fi
touch complins.dat
i=0
while read line
do
if [ $line == "SNA" ]; then
va=`grep -w "$line" datalins1.dat | awk BEGIN'{FS="\`~"}{if ( $3=="'$line'" ) {print $4}}'`
i=$(($i+1))
varits=$(echo $va|awk -v varif="$i" '{print $varif}')
if [ "$varits" = "DNA" ]; then
grep -w "$varits" datalins1.dat | awk BEGIN'{FS="`~"}{if ( $3=="'$line'" ) {print $1}}'|sed 's/$/~>/' >> complins.dat
else
grep -w "$varits" datalins1.dat | awk BEGIN'{FS="`~"}{if ( $3=="'$line'" ) {print $1}}'| tr '\n' '~' | sed 's/~/~>/g' >> complins.dat
while [ "$varits" != "DNA" ]
do
while read line
do
if [ $line == "$varits" ] ; then
varits=`grep -w "$line" datalins1.dat | awk BEGIN'{FS="\`~"}{if ( $3=='$line' ) {print $4}}'`
check=`echo $varits|awk '{print NF}'`
if [ "$check" == "1" ];then
echo "inside if"
grep -w "$varits" datalins1.dat | awk BEGIN'{FS="`~"}{if ( $3=='$line' ) {print $1}}'| tr '\n' '~'| sed 's/~/~>/' >> complins.dat
else
echo "problem occurs here in else case when $check > 1(ie. $4 from datalins1.dat has two number for same value of $1)"
fi
fi
done < source.dat
done
fi
echo "" >> complins.dat
fi
done < source.dat
}
complinagetest #calling function
--------------------------------------------------------------------------
input files for above function are datalins1.dat and source.dat
output files for above function is complins.dat
#datalins1.dat
14`~abc`~9`~11
14`~abc`~9`~10
36`~ee`~7`~9
36`~ee`~8`~9
58`~rtt`~12`~7
70`~gff`~13`~8
86`~tyu`~6`~12
86`~tyu`~6`~13
92`~mjh`~5`~6
28`~jkl`~10`~DNA
32`~mjk`~SNA`~5
82`~jkli`~11`~DNA
#source.dat
9
7
8
12
13
6
5
10
SNA
11
-----------------------------------------------------------------------------
Field Seperator: `~
The concept is to start with SNA in the 3rd Field.
Fetch the 4th field, search that 4th field content in the
third field of some other line , save the 1st field, get that line's 4th field and so on till
I reach DNA in the 4th field.
I have already developed part of the script which does that. But I am not sure how to handle, when I search for 4th field content in 3rd field, if it has more than one occurance.
------------------------------------------------------------------
Expected output for the above example:
#complins.dat
32~92~86~58~36~14~82
32~92~86~58~36~14~28
32~92~86~70~36~14~82
32~92~86~70~36~14~28
-------------------------------------------------------------------
the function works perfectly if we have single number in $4 of datalins1.dat for same value in $1
Could some one plz help me with this logic approach with unix script.
Thanks in Advance..