Friday, November 12, 2021

[SOLVED] Display Second Result of Duplicate Lines in Text File

Issue

I am trying to display the second result of duplicate lines in a text file.

Data: 60 60 61 64 63 78 78

Duplicate Lines: 60 60 78 78

Attempted Code: echo "60 60 61 64 63 78 78" | sed 's/ /\n/g' | uniq -D | tail -1

Current Result: 78

Expected Result: 60 78


Solution

You may try this gnu awk solution:

s='60 60 61 64 63 78 78'
awk -v RS='[[:space:]]+' '++fq[$0] == 2' <<< "$s"

60
78

To avoid getting line breaks after each line:

awk -v RS='[[:space:]]+' '++fq[$0] == 2 {printf "%s", $0 RT}' <<< "$s"

60 78


Answered By - anubhava