Reputation: 21
I have a list of words which I have sorted using the sort command and then printed all the duplicates using uniq -D
. The output file foo.txt
is as follows:
always
always
bat
bat
boot
boot
boot
hi
hi
I was then attempting to use the following command in a script to read the file with the tr
command.
It's not quite working, and the output looks the same as above.
This is what I have so far:
cat foo.txt | tr -s '\n'
The output, however, appears the same. My goal is for the output to be:
always always
bat bat
boot boot boot
hi hi
Upvotes: 0
Views: 602
Reputation: 2471
You can separate each group by a newline with uniq.
After that sed take this newline as field separator.
uniq --all-repeated=separate infile | sed ':A;$bB;/\n$/!{N;bA};:B;s/\n/ /g'
Upvotes: 0
Reputation: 5965
another awk
:
awk '{c=(($0!=p)?"\n":" "); printf c $0} {p=$0}' file
it prints newline at the beginning and no newline at end, so we can add a few more:
awk '{c=(($0!=p)?"\n":" "); p=$0} NR==1{printf $0;next} {printf c $0}
END {printf "\n"}' file
uniq -c file
instead of -D
which prints
2 always
2 bat
3 boot
2 hi
and then print the word for the times the first field says:
uniq -c file | awk '{for (i=1;i<$1;i++) printf $2 " "; print $2}'
Upvotes: 1