I'll try to be specific and clear.

I have a file: log.txt it contains multiple strings that I search to print and count each of them.

This is my command, only print columns coincidences in the log.txt file:

sed -n '1p' log.txt | awk '{ s = ""; for(i = 25; i <= NF; i++) s = s $i "\n"; print s}'


sed -n '1p' //prints the first line
awk '{ s = ""; for(i = 25; i <= NF; i++) s = s $i "\n"; print s}' //prints the next columns from the number 25 column


Column25 Column26 Column27 ColumnN <--#first filter:I need obtain specific headers.                    ColumnN 
Column25         Column27 ColumnN
        Column26 Column27  <--#Count how many times is repeat every string in whole file



I try to do: From the previous output I want to count all the coincidences in the same file file.log but in the same command:

sed -n '1p' log.txt | awk '{ s = ""; for(i = 25; i <= NF; i++) s = s $i "\n"; print s}'

and send again to the output like:

Desired Output:

Column25 - n times
Column26 - n times
Column27 - n times
Column28 - n times
Column29 - n times
ColumnN - n times

PS. I've thinking in use the same variable "$s" in the for loop to start a search, but is not working.

  • @steeldriver Check the desired output. Aug 9, 2018 at 16:52
  • @steeldriver There are an example to check. Aug 9, 2018 at 17:28
  • Why do you start at i=25 ? From the examples, it just looks like you want to count how many times everything from Column 1 to Column N reappears in the file, and yet the way this reads is that you want to start counting only things that appear after Column 25 Aug 9, 2018 at 17:40
  • @SergiyKolodyazhnyy, yep that's the idea, first take the headers from column 25, and then count in themselves how many times do they repeat themselves. I start to count from the column 25 because, in that column start the occurrences that I cares to me. Aug 9, 2018 at 17:44
  • OK, and you only want to take the columns from #25 and after and only count those ? So columns 1 - 25 should not be counted in whole file ? Aug 9, 2018 at 17:45

2 Answers 2


Here's how I'd approach this problem:

awk '{n=1;if(NR==1)n=25;for(i=n;i<=NF;i++) a[$i]++} END{for(val in a) print val,a[val]}' input.txt

The fact that you want to capture fields 25 and after in the first line, requires us to check NR variable, and set n variable which will be used in the loop. As for a[$i]++ that will be an associative array with fields being keys and values within the array will be their count incremented via ++ operator. This is a very typical method for counting fields in awk.

  • Yeah, that I want to do. But i've a doubt: Can add you line to my command? sed -n '1p' log.txt | awk '{ s = ""; for(i = 25; i <= NF; i++) s = s $i "\n"; print s}' or do I need to take your command alone? Aug 9, 2018 at 17:59
  • @Mareyes Alone. Just replace input.txt with your log.txt Aug 9, 2018 at 18:19
  • Create an infinite loop that counts all the file. prints all columns in the file. EDIT: I only add the sed -n '1p' before you command, allows the count but still print value 1 in occurrences. Aug 9, 2018 at 18:25

How about

awk '{for (i=25; i<=NF; i++) print $i; exit}' file | sort | uniq -c
  6 string1
  6 string2
  6 string3
  6 string4
  6 string5
  6 stringN

EDIT: In your newly added sample input, you don't have 24 fields to ignore before counting starts, and the limitation to the first line (as inferred from your first code snippet) seems to have gone as well. Try

tr -s ' ' '\n' <file |  sort | uniq -c
  2 Column1
  2 Column2
  3 Column3
  3 ColumnN
  • The output is only give to me one coincidence by string, like: 1 String1 1 String2 1 String3 Aug 9, 2018 at 16:31
  • Post your input (always wise to do)
    – RudiC
    Aug 9, 2018 at 16:41
  • That's I'm not looking for. Aug 9, 2018 at 16:45
  • I'm not the only one to ask for this.
    – RudiC
    Aug 9, 2018 at 16:57
  • Ok, in the file.log every string repeats more than once. I need the number of repetitions. Aug 9, 2018 at 17:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .