0

The thing I wanna do is to check if 2nd and the 3rd columns are repeating (if not repeating, take 4th, 5th and the 6th columns as they are), print the first element in 5th and the last element in 6th column with their corresponding 4th column.

Example input looks like this:

Chr1    39584   39678   Chr1    39653   39654
Chr1    39584   39678   Chr1    39654   39655
Chr1    39584   39678   Chr1    39677   39678
Chr1    40023   40039   Chr1    40038   40039
Chr1    283979  283993  Chr1    283979  283980
Chr1    283979  283993  Chr1    283980  283981
Chr1    283979  283993  Chr1    283989  283990
Chr1    283979  283993  Chr1    283990  283991
Chr1    283979  283993  Chr1    283992  283993

Desired output looks like this:

Chr1    39653   39678
Chr1    40038   40039
Chr1    283979  283993

Thanks!

4
  • Non-repeating columns should be kept too. I removed images so can you please take your thumbs down back if it was you :D No need to give it I assume..
    – obidobi
    Oct 9, 2020 at 11:58
  • 1
    Would you mind adding what you already tried, and where you failed? That way contributors can see what tools you have at your disposal, and you avoid receiving suggestions that you already know wouldn't work.
    – AdminBee
    Oct 9, 2020 at 12:00
  • I think I nailed it, not with a very pro solution but it works ahaha. here you go: awk '!a[$2]++ && !b[$3]++' input.txt | awk '{print $4"\t"$5"\t"$3}' > output.txt
    – obidobi
    Oct 9, 2020 at 12:13
  • 1
    Can you please edit your question so it actually asks what you need? You talk about taking "4th, 5th and the 6th columns as they are" and then "print the first element in 5th and the last element in 6th column with their corresponding 4th column", and then you give a solution that doesn't use the 6th column at all and instead uses the 4th, 5th and 3rd.
    – terdon
    Oct 9, 2020 at 12:35

1 Answer 1

0

I nailed it:

awk '!a[$2]++ && !b[$3]++' input.txt | cut -f 4,5 > first_part.txt

tac input.txt | awk '!a[$2]++ && !b[$3]' | tac | cut -f 6 > second_part.txt

paste first_part.txt second_part.txt > output.txt

but if you have fancier solutions, do not hesitate to share!

7
  • 1
    You're printing the value from the 3rd column, not the 6th. That would work if you can guarantee that the maximum value in the last column for a pair of the 2nd and 3rd column values is the value in the 3rd column. This guarantee was not given in the question.
    – Kusalananda
    Oct 9, 2020 at 12:20
  • 1
    You don't need the second awk: awk -v OFS="\t" '!a[$2]++ && !b[$3]++{print $4,$5,$3}' input.txt.
    – terdon
    Oct 9, 2020 at 12:24
  • 1) "print the first element in 5th and the last element in 6th column with their corresponding 4th column" - where is that in your solution? 2) "check if 2nd and the 3rd columns are repeating" - when the columns are repeating? If the 2nd column has same values in same order as the 3rd column?
    – MiniMax
    Oct 9, 2020 at 12:32
  • Is it sure that the 4th column is the same for all lines with "repeating 2nd and 3rd" columns? If not, which value to choose for the 4th column?
    – AdminBee
    Oct 9, 2020 at 12:58
  • 1
    @obidobi You have received several requests for clarification; it is unlikely that someone contributes a solution unless they understand exactly the task at hand ...
    – AdminBee
    Oct 9, 2020 at 14:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .