Counting Lines Starting with a Certain Word

Counting lines starting with a certain word

Try this:-

  awk '/^yourwordtofind/{a++}END{print a}' file

How to count the number of lines that a specific word occurs in a txt file?

You could probably have an integer counter inside the if statement. It would iterate just before/after the line is printed with ++.

Count the number of words in the line beginning with a particular word

Try that:

/^.*?\*AUY:(.*?)$/gmi

Explanation

  1. ^ asserts position at start of a line
  2. .*? matches any character (except for line terminators)
  3. *? Quantifier — Matches between zero and unlimited times (lazy)
  4. \* matches the character *
  5. AUY: matches the characters AUY
  6. .*? matches any character (except for line terminators)
  7. $ asserts position at the end of a line
  8. g modifier: global. don't return after first match
  9. m modifier: multi line. Causes ^ and $ to match the begin/end of
    each line (not only begin/end of string)
  10. i modifier: insensitive

Rubular

Code Sample:

function countWord(){

const regex = /^.*?\*AUY:(.*?)$/gmi;
const str = `*AUY: today is holiday so Peter and Mary do not need to go to work .
%mor: n|today cop|be&3s n|holiday conj|so n:prop|Peter conj|and n:prop|Mary v|do neg|not v|need inf|to v|go prep|to n|work .
%snd: <00:00:00><00:07:37>
%AUY: ok_pfp (0.40) er today is holiday errfr ::: so er Peter and Mary {is} ~ er do not need errfr ::: to go to work . errfr :;:a |`;
let m;

while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
alert(m[1].match(/\b(\w+)\b/g).length);
}

}

Python: count how many lines have specific words

Problem is file.read() return individual characters so if print it you will understand so to deal with this problem we are using file.readlines() so the below code works absolutely fine

count=0
lines=file.readlines()
for line in lines:
if any(word in line for word in words):
count+=1
print(count)

Linux count number of lines a specific word occurs in a text file based on certain date and time stamp

The problem with your approach is that you are looking for a pattern which contains [ and ]. These have a special meaning in regular expressions, namely they are "character lists" and match any character enclosed in them. So, performing

grep '[com.java.Name abc]' logfile

would match any line containing any of the characters a, b, c, e, j, m, o, v, N, the space, and the period ., regardless of their location on the line (which likely matches every line of the log file).

You need to escape the [ and ], as in

grep -c '\[com.java.Name abc\]' logfile

or - as pointed out by @terdon - use the -F flag:

grep -c -F '[com.java.Name abc]' logfile

If you want to look for occurences at a certain date, the mechanism depends. If you know the day, say 2020-06-14, it could be as easy as stating

grep -c '^2020-06-14.*\[com.java.Name abc\]' logfile

If you want to search based on the full timestamp, that approach would only work if you knew the exact moment as it is formatted in the logfile, as in

grep -c '^2020-06-14 13:48:12,442.*\[com.java.Name abc\]' logfile

which is unlikely because then you probably wouldn't need to count the occurences in the first place. In that case, you could try to adapt some of the following answers:

  • Extracting between specific time lines from log file with grep and awk
  • grep particular log entry greater than specific time

How to count lines in a document?

Use wc:

wc -l <filename>

This will output the number of lines in <filename>:

$ wc -l /dir/file.txt
3272485 /dir/file.txt

Or, to omit the <filename> from the result use wc -l < <filename>:

$ wc -l < /dir/file.txt
3272485

You can also pipe data to wc as well:

$ cat /dir/file.txt | wc -l
3272485
$ curl yahoo.com --silent | wc -l
63

Read text file line by line and Count specific word for particular values from a line and save the line above that

Problem Solved. Thanks to my friend & @RomanPerekhrest:

    with open('Test.txt') as f:
for line in f:
line = line.strip()
if line:
if line.startswith('Block'):
Rcount, found = 0, False
elif 'Read time' in line and not found:
readLine = line
Rcount += 1
nextLine = next(f)
if 'FBC' in nextLine and not found:
num = pat.search(nextLine).groups()
num = ''.join(map(str, num))
if int(num) >= 500:
found = True
print Rcount
print readLine
TotalCount.append(count)

O/P:

3

Read time= 1623, Cycle= 1,DFFFBFFF,FFBBFFFF,FCFFFF8F,FDFBFFCC,

4

Read time= 1624, Cycle= 1,DFFFFFFC,FFBBFFFB,FFEFFF8F,FDFBFDDD,



Related Topics



Leave a reply



Submit