<file2> : same as file1, but can have extra lds and also same Ids with different words belonging to that Id
niLM FY4 gt45
piMN-1 pqr ss3 abc
aiPQ44 asf ggy-9
I need an output file which checks for all the Ids of file2 in file1 and if matches, it checks for the associated words and prints only the matched words (order doesn�t matter). If the Id of file2 does not exist in file1 or do not match with any of its words, then print only the Id.
#!/usr/bin/ksh
typeset -i mCnt
while read mLine
do
mOutLine=""
for mFld in ${mLine}
do
if [[ "${mOutLine}" = "" ]]; then
mOutLine=${mFld}
mTag=${mFld}
continue
fi
mCnt=$(egrep -c "${mTag}.*${mFld}" target_file)
if [[ ${mCnt} -ne 0 ]]; then
mOutLine=${mOutLine}' '${mFld}
fi
done
echo $mOutLine
done < source_file
#!/bin/bash
while read -r linef; do
la=();
m1=$(echo $linef|sed -n "s/\([^ ]*\).*/\1/p ")
while read -r lineff; do
m2=$(echo $lineff|sed -n 's/\([^ ]*\).*/\1/p ')
if [[ "$m1" = "$m2" ]] ; then
for i in $(echo "$lineff $linef"|sed 's/ /\n/g')
do
la=(${la[@]} $i)
done
x=0
y=$(echo ${#la[@]} )
while [ $(( y -= 1 )) -gt -1 ]
do
for i in ${la[@]}
do
if [ ${la[x]} = $i ] ; then
((c++))
fi
done
if [ $c -ge 2 ] ; then
added=(${added[@]} ${la[x]})
fi
for i in ${added[@]}
do
if [ $i = ${la[x]} ] ; then
((adc++))
fi
done
if [[ $adc -gt 1 ]] ; then
ar=(${ar[@]} ${la[x]})
fi
adc=0 ;((x++)); c=0
done
echo ${ar[@]}
ne=0
else
ne=1
fi
done<file1
if [ $ne -eq 1 ] ; then
if [[ ${ar[0]} != $m1 ]] ; then
echo $m1
fi
fi
ar=();eq=1
done<file2