1

I want to have exactly the same output in this post(Extract data in linux/unix above,but I have a new input file,which is below:

ABCB11  4   ACE 11
ABCB11  4   CHRM1   114
ABCB11  4   CHRM2   115
ABCB11  4   DRD2    158
ABCB11  4   EGF 164
ABCC8   5   ACE 11
ABCC8   5   ADRA1A  21
ABCC8   5   ADRA1B  22
ABCC8   5   ADRA1D  23
ABCC8   5   CHRM1   114

Taking all the unique genes and creating the output.

I want all rows to have fromid,after taking distinct values from both. The exact output is what I want below:

ABCB11 = fromid=4,from=ABCB11
ABCC8 = fromid=5,from=ABCC8
ACE = fromid=11,from=ACE
CHRM1 = fromid=114,from=CHRM1
CHRM2 = fromid=115,from=CHRM2
DRD2 = fromid=158,from=DRD2
EGF = fromid=164,from=EGF
ADRA1A = fromid=21,from=ADRA1A
ADRA1B = fromid=22,from=ADRA1B
ADRA1D = fromid=23,from=ADRA1D
1

Assuming 4 columns, a two-pass solution

awk 'NR == FNR {if (s[$1]++ == 0)
{printf "%s = fromid=%s, from=%s\n", $1, $2, $1}; next};
!s[$3]++{printf "%s = fromid=%s, from=%s\n", $3, $4, $3}' file file
ABCB11 = fromid=4, from=ABCB11
ABCC8 = fromid=5, from=ABCC8
ACE = fromid=11, from=ACE
CHRM1 = fromid=114, from=CHRM1
CHRM2 = fromid=115, from=CHRM2
DRD2 = fromid=158, from=DRD2
EGF = fromid=164, from=EGF
ADRA1A = fromid=21, from=ADRA1A
ADRA1B = fromid=22, from=ADRA1B
ADRA1D = fromid=23, from=ADRA1D
1

If you don't mind the order of ouput:

$ awk '!($1 in a){a[$1] = $2}
       !($3 in a){a[$3] = $4}
       END {
           for(i in a) {
               print i" = fromid="a[i]",from="i
           }
       }' file
EGF = fromid=164,from=EGF
CHRM1 = fromid=114,from=CHRM1
CHRM2 = fromid=115,from=CHRM2
ACE = fromid=11,from=ACE
ADRA1A = fromid=21,from=ADRA1A
DRD2 = fromid=158,from=DRD2
ABCB11 = fromid=4,from=ABCB11
ADRA1B = fromid=22,from=ADRA1B
ABCC8 = fromid=5,from=ABCC8
ADRA1D = fromid=23,from=ADRA1D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.