lundi 13 avril 2020

Counting with a complicated conditions using awk

I have a task. I must count inner and outer water bridges from data. I want to show you how I should count them.

For example, I have a data file:

MGD12   SOL54  
MGD14   SOL74    
MGD10   SOL37    
MGD16   SOL65    
MGD21   SOL66
MGD2    SOL65    
MGD64   SOL74    
MGD10   SOL37    
MGD72   SOL74    
MGD12   SOL54

Inner water bridges are when: MGD and SOL are the same (duplicates). Outer water bridges are when: MGD is different, but SOL is the same.

For example, in the third column, I write which line is inner water bridge and which is outer

1.MGD12    SOL54    inner (the same in line 10)
2.MGD14    SOL74    outer (the same SOL in 7, 9)
3.MGD10    SOL37    inner (the same in line 8)
4.MGD16    SOL65    outer (the same SOL in 6)
5.MGD21    SOL66    no water bridge
6.MGD2     SOL65    outer (the same SOL in 4)
7.MGD64    SOL74    outer (the same SOL in 2, 9)
8.MGD10    SOL37    inner (the same in line 3)
9.MGD72    SOL74    outer (the same SOL in 2, 7)
10.MGD12   SOL54    inner (the same in line 1)

In the output, I want just the number of inner and outer water bridges. In this case, it will be only numbers 4 and 5.

4 5

I try to write a script, but I don't know what I should put in condition, maybe I should use arrays?

#!/bin/bash
awk '{ if () inner++; else if () outer++} END { print inner " " outer}' probe.txt

Aucun commentaire:

Enregistrer un commentaire