C. Monster C. Monster - 5 months ago 8
Bash Question

Using shell commands within an awk script which must access the awk commands

This is essentially the command I want, All of it works except that I want to print something special in my third column that would use shell commands(or just more awk commands I guess but I don't know how I would fit this into the original awk statement). All I need help with is the pseudo command substitution between $2, and ar[$4,$1] in the print statement but left the rest in for the sake of specificity.

awk 'NR==FNR{ar[$3,$2]=$1+ar[$3,$2]; }
NR>FNR && ar[$4,$1] {print "hs"$1,$2,`awk '$1 == #$1 from outer awk command# file2 | tail -n 1 | awk '{print $3}'`, ar[$4,$1]}' file1 file2


file1 will look like

5 8 t11
15 7 t12


file2 will look like

8 4520 5560 t11
8 5560 6610 t12
8 6610 7400 t13
7 9350 10610 t11
7 10610 11770 t12
7 11770 14627 t13
7 14627 16789 t14


And output should look like

8 4520 7400 5
7 10610 16789 15


Thank-You!

agc agc
Answer

Non-awk, inefficient shell tools code:

while read a b c ; do \
    echo -n "$b " ; \
    egrep "^$b " file2 | \
      grep -A 9999999 " $c" | \
      cut -d' ' -f2,3 | \
      sed '1{s/ .*//;t}
           ${s/.* //;t};d' | \
      xargs echo -n  ; \
    echo " $a" ; \
done < file1 | \
  column -t

Output:

8  4520  7400   5
7  10610 16789  15