开发者

tail file till last duplicate line in a file using bash

Hey everyone! How to in simple way find line number of开发者_如何转开发 last duplicate in file I need take tale till last duplicate Example

hhhh
 str1
 str2
hhhh
 str1
hhh
**str1
str2
str3**

I need only bold till hhh(str1,str2,str3).Thanks in advance!


Give this a try:

awk '{if (a[$0]) accum = nl = ""; else {a[$0]=1;accum = accum nl $0; nl = "\n"}} END { print accum}' inputfile

Given this input:

aaa
b
c
aaa
d
e
f
aaa
b
aaa
g
h
i

This is the output:

g
h
i


taking the sample from Dennis,

$ gawk -vRS="aaa" 'END{print}'  file

g
h
i

here's another way if you don't know before hand, although not as elegant as one awk script.

var=$(sort file| uniq -c|sort -n | tail -1| awk '{print $2}')
gawk -vRS="$var" 'END{print}'  file

still, this will only get the duplicate that occurs the most frequency. it does not get the "last duplicate" , whatever that means.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜