Getting repeated lines with awk in Bash

162 views Asked by At

I'm trying to know which are the lines that are repeated X times in a text file, and I'm using awk but I see that awk in my command, not work with lines that begin with the same characters or words. That is, does not recognize the full line individually.

Using this command I try to get the lines that are repeated 3 times:

 awk '++A[$1]==3' ./textfile > ./log
2

There are 2 answers

0
Arjun Mathew Dan On BEST ANSWER

This is what you need hopefully:

awk '{a[$0]++}END{for(i in a){if(a[i]==3)print i}}' File

Increment array a with the line($0) as index for each line. In the end, for each index ($0), check if the count(a[i] which is the original a[$0]) equals 3. If so, print the line (i which is the original $0 / line). Hope it's clear.

0
user2138595 On

This returns lines repeated 3 times but adds a space at the beginning of each 3x-repeated line:

sort ./textfile | uniq -c | awk '$1 == 3 {$1 = ""; print}' > ./log