arnpry
arnpry

Reputation: 1141

Display Second Result of Duplicate Lines in Text File

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

Upvotes: 1

Views: 65

Answers (4)

Ed Morton
Ed Morton

Reputation: 203502

$ printf '%s\n' 60 60 61 64 63 78 78 | uniq -d
60
78

The above is assuming a hard-coded list of numbers works for you since, per the example in your question, you thought echo "60 60 61 64 63 78 78" | sed 's/ /\n/g' | uniq -D | tail -1 would work for you. If you need to store them in a variable first then make it an array variable:

$ vals=(60 60 60 61 64 63 78 78)
$ printf '%s\n' "${vals[@]}" | uniq -d
60
78

Upvotes: 0

KamilCuk
KamilCuk

Reputation: 141010

So if you do not want to then do not use tail -1. And instead of printing all, print duplicates once per duplicate.

echo "60 60 61 64 63 78 78" | sed 's/ /\n/g' | sort | uniq -d

Note - if the input is not sorted (or duplicate lines are not adjacent), you need to sort it first.

Upvotes: 0

anubhava
anubhava

Reputation: 785146

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

Upvotes: 4

RavinderSingh13
RavinderSingh13

Reputation: 133518

Considering that you could have multiple lines in your Input_file then you could try following.

awk '
{
  delete value
  num=split($0,arr," ")
  for(i=1;i<=num;i++){
    value[arr[i]]++
  }
  for(i=1;i<=num;i++){
    if(value[arr[i]]>1){
      print arr[i]
      delete value[arr[i]]
    }
  }
}
'  Input_file

Explanation: Adding detailed explanation for above.

awk '                          ##Starting awk program from here.
{
  delete value                 ##Deleting value array here.
  num=split($0,arr," ")        ##Splitting current line to array arr here.
  for(i=1;i<=num;i++){         ##Traversing through all fields here.
    value[arr[i]]++            ##Creating value array with value of arr array.
  }
  for(i=1;i<=num;i++){         ##Traversing through all fields here.
    if(value[arr[i]]>1){       ##Checking condition if value array value is coming more than 1 times then do following.
      print arr[i]             ##printing array value here(which is value which comes more than 1 time).
      delete value[arr[i]]     ##Deleting value array value to avoid duplicate printing here.
    }
  }
}
'  Input_file                  ##Mentioning Input_file name here.

Upvotes: 1

Related Questions