#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    Common fields multiple files


    Hi,

    I have multiple files like:

    Code:
    chr1 pos1 value1
    chr1 pos2 value2
    chr1 pos3 value3
    chr2 pos1 value4
    chr3 pos1 value5
    I would like an output like:

    Code:
    chr1 pos1 value1 value1 value1
    chr1 pos3 value3 value3 value3
    chr2 pos3 value4 value4 value4
    If field1 and field2 match in all the files, print field1, field2 and field3.

    I know how to do it with 2 files:

    Code:
    awk 'FNR==NR{a[$1" "$2]=$3;next} $1" "$2 in a {print $1,$2,$3,a[$1" "$2]}' file2 file1
    How can I modify the above code to do it with multiple files?
    thanks,
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    852
    Rep Power
    387

    Thumbs down


    Originally Posted by aec
    Hi,

    I have multiple files like:
    . . . E t c . . .
    How can I modify the above code to do it with multiple files?
    thanks,
    Which "multiple" files?
    You need to post sample data with the expected results from that data.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    I forgot to write as 'code', now it is better?
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    852
    Rep Power
    387

    Thumbs down


    Originally Posted by aec
    I forgot to write as 'code', now it is better?
    I still see but one file.
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    852
    Rep Power
    387

    Cool


    Originally Posted by aec
    I forgot to write as 'code', now it is better?
    Is this what you need?:
    Code:
    ==> cat m1
    cat - <<! >File1
    chr1 pos1 value1
    chr1 pos2 value2
    chr1 pos3 value3
    chr2 pos1 value4
    chr3 pos1 value5
    !
    cat - <<! >File2
    chr1 pos1 f2val1
    chr1 pos2 f2val2
    chr1 pos3 f2val3
    chr2 pos1 f2val4
    chr3 pos1 f2val5
    !
    cat - <<! >File3
    chr1 pos1 f3val1
    chr1 pos2 f3val2
    chr1 pos3 f3val3
    chr2 pos1 f3val4
    chr3 pos1 f3val5
    !
    
    awk '{a[$1" "$2]=a[$1" "$2]" "$3} END { for (x in a) {print x,a[x]}}' File?
    Result is:
    Code:
    ==> ./m1
    chr1 pos1  value1 f2val1 f3val1
    chr2 pos1  value4 f2val4 f3val4
    chr1 pos2  value2 f2val2 f3val2
    chr3 pos1  value5 f2val5 f3val5
    chr1 pos3  value3 f2val3 f3val3

IMN logo majestic logo threadwatch logo seochat tools logo