vikus
May 8, 2014, 2:54pm
1
I have 2 files:
fileA
AAA1:AAA2:AAA3:AAA_4:AAA5:AAA_6:AAA7:AAA_8
BBB1:BBB2:BBB3:BBB_4:BBB5:BBB-6
CCC1:CCC2:CCC3:CCC_4
fileB
AAA_4:XXX1:YYY1
BBB_4:XXX2:YYY2
CCC_4:XXX3:YYY3:ZZZ3
AAA_6:XXX4:YYY4
AAA_8:XXX5:YYY5
Result:
AAA1:AAA2:AAA3:AAA_4:XXX1:YYY1:AAA5:AAA_6:XXX4:YYY4:AAA7:AAA_8:XXX5:YYY5
BBB1:BBB2:BBB3:BBB_4:XXX2:YYY2:BBB5:BBB-6
CCC1:CCC2:CCC3:CCC_4:XXX3:YYY3:ZZZ3
If value from first column (fileA) exist in second file (fileB), copy everything from that line after the appearance of value from first column fileA.
Can some help with some solution, preferred awk programming.
awk -F: 'FNR==NR{o=$1;sub("^[^"FS"]*"FS,"");f2[o]=$0;next}{for(i=1;i<=NF;i++) if($i in f2) $i=$i FS f2[$i]}1' OFS=: fileB fileA
Ditto
May 8, 2014, 3:09pm
3
vikus:
I have 2 files:
fileA
AAA1:AAA2:AAA3:AAA_4:AAA5:AAA_6:AAA7:AAA_8
BBB1:BBB2:BBB3:BBB_4:BBB5:BBB-6
CCC1:CCC2:CCC3:CCC_4
fileB
AAA_4:XXX1:YYY1
BBB_4:XXX2:YYY2
CCC_4:XXX3:YYY3:ZZZ3
AAA_6:XXX4:YYY4
AAA_8:XXX5:YYY5
Result:
AAA1:AAA2:AAA3:AAA_4:XXX1:YYY1:AAA5:AAA_6:XXX4:YYY4:AAA7:AAA_8:XXX5:YYY5
BBB1:BBB2:BBB3:BBB_4:XXX2:YYY2:BBB5:BBB-6
CCC1:CCC2:CCC3:CCC_4:XXX3:YYY3:ZZZ3
If value from first column (fileA) exist in second file (fileB), copy everything from that line after the appearance of value from first column fileA.
Can some help with some solution, preferred awk programming.
Well, no awk involved, but this seems to work
(I'm not that good with awk myself ... *shrug* )
for i in `cat fileB`
do
s=`echo $i | cut -d: -f1`
sed "s/$s/$i/g" fileA > fileA.2
mv fileA.2 fileA
done
try also:
awk -F: '
NR==FNR {a[c++]=$0; next}
{for (i=0; i<c; i++) if (match(a, $1)) gsub($1, $0, a);}
END {for (i=0; i<c; i++) print a}
' fileA fileB
vikus
May 9, 2014, 3:08am
5
Thank you all
Now some more condition if value from fileB have no any more columns, add to fileA string "NA:NA"
I have 2 files:
fileA
AAA1:AAA2:AAA3:AAA_4:AAA5:AAA_6:AAA7:AAA_8
BBB1:BBB2:BBB3:BBB_4:BBB5:BBB-6
CCC1:CCC2:CCC3:CCC_4
fileB
AAA_4:XXX1:YYY1
BBB_4:XXX2:YYY2
CCC_4:XXX3:YYY3:ZZZ3
AAA_6:XXX4:YYY4
AAA_8:
Result:
AAA1:AAA2:AAA3:AAA_4:XXX1:YYY1:AAA5:AAA_6:XXX4:YYY4:AAA7:AAA_8:NA:NA
BBB1:BBB2:BBB3:BBB_4:XXX2:YYY2:BBB5:BBB-6
CCC1:CCC2:CCC3:CCC_4:XXX3:YYY3:ZZZ3
If there are any other conditions, now's the time to share them.
Regards,
Alister
vikus
May 9, 2014, 4:37am
7
that would be all, no more conditions
Try:
awk 'NR==FNR{i=$1; $1=x; A=($2=="")?":NA:NA":$0; next} {for(i=1; i<=NF; i++) $i=$i A[$i]}1' FS=: OFS=: fileB fileA
vikus
May 9, 2014, 6:51am
9
Working exactly as should be, Thank you.