The following should do what you want, assuming your file is called Input.txt
.
uniq -d Input.txt | xargs -I {} grep {} Input.txt
xargs -I {}
basically tells xargs
to substitute the input that is being piped in whenever it sees {}
in a later command.
grep {} Input.txt
will be called with each line of input from the pipe, where the line of input will get substituted where {}
is.
Why does this work? We are using uniq -d
to find the duplicate entries, and then using them as input patterns to grep
to match all the lines which contain those entries. Thus, only duplicate entries are printed, and they are printed exactly as many times as they appear in the file.
Update: Printing the duplicates occurences only, not the first occurrence, in a way that is compatible with ksh
, since the OP does not apparently have bash
on his system.
uniq -d Input.txt | xargs -L 1 | while read line
do
grep "$line" Input.txt | tail -n +2;
done
Note that in the above scripts, we are assuming that no line is a substring of another line.