Count every string of awk output search in a file

2018-08-09 18:10:16

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}'

Explanation

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

Input:

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

Output:

string1

string2

string3

string4

string5

stringN

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

  • 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'

    2 Column1

    2 Column2

    3 Column3

    3 ColumnN

    2018-08-09 19:31:40
  • 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.

    2018-08-09 20:07:21