how to count number of lines of a specific entry under a specific pattern using awk?

awk match pattern in column
awk pattern matching if condition
awk: count occurrences of string
awk count number of occurrences in a line
awk print column matching pattern
awk match function
linux count occurrences of string in line
awk count lines

I have a text file with a pattern that looks like the following

Sample1
Feature 1
A
B
C
Feature 2
A
G
H
L
Sample2
Feature 1
A
M
W
Feature 2
P
L

I'm trying to count how many entries are for each feature in each sample. So my desired output should look something like this:

Sample1
Feature 1: 3
Feature 2: 4

Sample2
Feature 1: 3
Feature 2: 2

I tried using the following awk command:

$ awk '{if(/^\Feature/){n=$0;}else{l[n]++}}
       END{for(n in l){print n" : "l[n]}}' inputfile.txt > result.txt

But it gave me the following output

Feature 1: 6
Feature 2: 6

So I was wondering if someone can help me in modifying this command to get the desired output or suggest for me another command? (P.S the original file contains hundreds of samples and around 94 features)

You could use this awk:

awk '/^Sample/{printf "%s%s",(c?c"\n":""),$0;c=0;next}
     /^Feature/{printf "%s\n%s: ",(c?c:""),$0;c=0;next}
     {c++}
     END{print c}' file

The script increment the counter c only for lines that doesn't start with Sample or Feature.

If one of the 2 keywords are found, the counter is printed.

Count records matching pattern with Awk, To get you started you can use awk to search for lines in a file that contain a string like so: you'll stumble upon this SO Q&A titled: using awk to count no of records. further restrict the awk command to search only that field like so: the given chars or $1~/^prefix/ to match only names starting with prefix : how to count number of lines of a specific entry under a specific pattern using awk? Ask Question Asked 1 year, Count number of lines in a git repository. 21.

This awk may also work:

awk '/^Sample/ {
   for (i in a)
      print i ": " a[i]
   print
   delete a
   next
}
/^Feature/ {
   f = $0
   next
}
{
   ++a[f]
}
END {
   for (i in a) 
      print i ": " a[i]
}' file

Sample1
Feature 1: 3
Feature 2: 4
Sample2
Feature 1: 3
Feature 2: 2

Matching Patterns and Processing Information with awk, Processing input to find numeric counts, sums, or subtotals. Verifying that a given field contains only numeric information For each matching record, it will print two lines, first the number of the record on which the match was made and then  This chapter describes the awk command, a tool with the ability to match lines of text in a file and a set of commands that you can use to manipulate the matched lines. In addition to matching text with the full set of extended regular expressions described in Chapter 1, awk treats each line, or record, as a set of elements, or fields, that can be manipulated individually or in combination.

$ cat tst.awk
BEGIN { OFS = ": " }
/Sample/  { prtFeat(); print (NR>1 ? ORS : "") $0; next }
/Feature/ { prtFeat(); name=$0; next }
{ ++cnt }
END { prtFeat() }
function prtFeat() {
    if (cnt) {
        print name, cnt
        cnt = 0
    }
}

$ awk -f tst.awk file
Sample1
Feature 1: 3
Feature 2: 4

Sample2
Feature 1: 3
Feature 2: 2

AWK Cheat Sheet, awk '/pattern/ {action}' file↵. Execute action for matched pattern 'pattern' on file '​file'. ;. Char to separate two actions. print. Print current record line. $0. Reference​  Checkout the printing section on the AWK user guide for more information on this. Now we’ve selected a couple of columns to print out, let’s use AWK to search for a specific thing – a number we know exists in the dataset. Note that if you specify what fields to print out, AWK will print the whole line that matches the search by default.

How to Count the Number of lines, Words, and, Characters in a Text , Count Number of Lines in a Text File. use the “wc” command on terminal and find the number of files (or files of certain type) in a directory. The accepted answer is almost complete you might want to add an extra sort -nr at the end to sort the results with the lines that occur most often first. -c, --count prefix lines by the number of occurrences. -n, --numeric-sort compare according to string numerical value -r, --reverse reverse the result of comparisons.

[PDF] grep, awk and sed – three VERY useful command-line utilities Matt , In this particular case, it will print If you want a wider range of regular expression commands then you must use 'grep -E' (also known For each line of the input file, it sees if there are any pattern-matching instructions, in Hence it would be straightforwards to write an awk command that would calculate the mean and. The awk command is included by default in all modern Linux systems, so we do not need to install it to begin using it. Awk is most useful when handling text files that are formatted in a predictable way. For instance, it is excellent at parsing and manipulating tabular data. It operates on a line-by-line basis and iterates through the entire file.

How to count the occurrence of specific string on a specific line in a , Search the line for all occurrences of a specific pattern (here the string/regular expression Grep's output is fed to wc, which counts the number of lines. single quotes;; the input file input.txt is redirected into stdin stream of the python interpreter via < shell operator. awk 'NR==2 { print gsub("f",""); }' file 3. How can I skip the first 6 lines/rows in a text file (input.txt) and process the rest with awk? The format of my awk script (program.awk) is: BEGIN { } { process here } END { } My text file

Comments
  • Why did you put a backslash before Feature in if(/^\Feature/)? Btw, never use the letter l as a variable name is it looks far too much like the number 1 and so obfuscates your code.
  • @EdMorton I don't have much experience in awk commands and this command was suggested in a previous post. Thanks for the note about the letter "l", i will avoid using it.
  • Thanks a lot for your help and explanation! The command worked very well!
  • Worked perfectly! Thanks for your help!