<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic counting the number of times a word appears in a file in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634807#M105004</link>
    <description>I have a extracted log file and with the word smtp or SMTP appearing either once or multiple times per line, is there a way that we can count how many times smtp appears in the log, can't just count the lines because it appears more than once on certain lines...</description>
    <pubDate>Mon, 26 Sep 2005 10:51:13 GMT</pubDate>
    <dc:creator>Belinda Dermody</dc:creator>
    <dc:date>2005-09-26T10:51:13Z</dc:date>
    <item>
      <title>counting the number of times a word appears in a file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634807#M105004</link>
      <description>I have a extracted log file and with the word smtp or SMTP appearing either once or multiple times per line, is there a way that we can count how many times smtp appears in the log, can't just count the lines because it appears more than once on certain lines...</description>
      <pubDate>Mon, 26 Sep 2005 10:51:13 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634807#M105004</guid>
      <dc:creator>Belinda Dermody</dc:creator>
      <dc:date>2005-09-26T10:51:13Z</dc:date>
    </item>
    <item>
      <title>Re: counting the number of times a word appears in a file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634808#M105005</link>
      <description>grep -i smtp |wc  -w&lt;BR /&gt;&lt;BR /&gt;ought to do it.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Pete</description>
      <pubDate>Mon, 26 Sep 2005 10:54:05 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634808#M105005</guid>
      <dc:creator>Pete Randall</dc:creator>
      <dc:date>2005-09-26T10:54:05Z</dc:date>
    </item>
    <item>
      <title>Re: counting the number of times a word appears in a file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634809#M105006</link>
      <description>Sorry Pete, I tried that earlier, it grabs the lines with smtp and counts all the words in the line...</description>
      <pubDate>Mon, 26 Sep 2005 10:57:30 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634809#M105006</guid>
      <dc:creator>Belinda Dermody</dc:creator>
      <dc:date>2005-09-26T10:57:30Z</dc:date>
    </item>
    <item>
      <title>Re: counting the number of times a word appears in a file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634810#M105007</link>
      <description>If the word smtp|SMTP is having any delimiter , if so you can write a script which search for each word compare the word and then incriment a counter if the word is matched .&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Thanks,&lt;BR /&gt;BL.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 26 Sep 2005 11:04:39 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634810#M105007</guid>
      <dc:creator>baiju_3</dc:creator>
      <dc:date>2005-09-26T11:04:39Z</dc:date>
    </item>
    <item>
      <title>Re: counting the number of times a word appears in a file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634811#M105008</link>
      <description>James,&lt;BR /&gt;&lt;BR /&gt;ARGHH!  You're right of course.  I remember a similar question a month or so ago but I can't find it at the moment and don't remember what the answer was.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Pete</description>
      <pubDate>Mon, 26 Sep 2005 11:05:56 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634811#M105008</guid>
      <dc:creator>Pete Randall</dc:creator>
      <dc:date>2005-09-26T11:05:56Z</dc:date>
    </item>
    <item>
      <title>Re: counting the number of times a word appears in a file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634812#M105009</link>
      <description>HI James,&lt;BR /&gt;&lt;BR /&gt;You can still use grep &amp;amp; wc -w&lt;BR /&gt;Save the grepped lines in a tmp file &lt;BR /&gt;Then just read through that file in a loop and  increment a counter from the wc -w output.&lt;BR /&gt;Should work.&lt;BR /&gt;&lt;BR /&gt;Rgds,&lt;BR /&gt;Jeff</description>
      <pubDate>Mon, 26 Sep 2005 11:11:11 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634812#M105009</guid>
      <dc:creator>Jeff Schussele</dc:creator>
      <dc:date>2005-09-26T11:11:11Z</dc:date>
    </item>
    <item>
      <title>Re: counting the number of times a word appears in a file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634813#M105010</link>
      <description>There is no special delimiter and the fields are not fixed, there could be a % ; or even a white space before the smtp or SMTP. &lt;BR /&gt;&lt;BR /&gt;Jeff I do not understand your response, even if I throw all the lines with either smtp | SMTP into an output file I will still have all the other words (mail stuff that would come up in the wc command.</description>
      <pubDate>Mon, 26 Sep 2005 11:19:17 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634813#M105010</guid>
      <dc:creator>Belinda Dermody</dc:creator>
      <dc:date>2005-09-26T11:19:17Z</dc:date>
    </item>
    <item>
      <title>Re: counting the number of times a word appears in a file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634814#M105011</link>
      <description>a quick awk script&lt;BR /&gt;&lt;BR /&gt;cat file | awk '{&lt;BR /&gt;for ( i=1; i&amp;lt;=NF; i++) &lt;BR /&gt;  num[$i]++;&lt;BR /&gt;}&lt;BR /&gt;END {&lt;BR /&gt;for ( word in num )&lt;BR /&gt;  print word, num[word];&lt;BR /&gt;}' | grep -i smtp</description>
      <pubDate>Mon, 26 Sep 2005 11:19:18 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634814#M105011</guid>
      <dc:creator>curt larson_1</dc:creator>
      <dc:date>2005-09-26T11:19:18Z</dc:date>
    </item>
    <item>
      <title>Re: counting the number of times a word appears in a file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634815#M105012</link>
      <description>another method&lt;BR /&gt;&lt;BR /&gt;cat file | tr "[:upper:]" "[:lower:]" |&lt;BR /&gt;tr -cs "[a-z0-9']" "\012" | sort |&lt;BR /&gt;uniq -c | sort +0nr +1d&lt;BR /&gt;&lt;BR /&gt;convert all uppercase to lowercase&lt;BR /&gt;replace all characters not a-z0-9' with a new line. that means one word per line&lt;BR /&gt;sort because uniq expects sorted input&lt;BR /&gt;uniq counts the number of times each word appears then sort first from most to least frequent then alphabetically</description>
      <pubDate>Mon, 26 Sep 2005 11:24:59 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634815#M105012</guid>
      <dc:creator>curt larson_1</dc:creator>
      <dc:date>2005-09-26T11:24:59Z</dc:date>
    </item>
    <item>
      <title>Re: counting the number of times a word appears in a file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634816#M105013</link>
      <description>One way (though you'll have to change it to cope with uppercase SMTP to &lt;BR /&gt;&lt;BR /&gt;exactly as typed, including the '\' !!&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;# sed -e 's/smtp/smtp\&lt;BR /&gt;/g' your_file | grep -i "smtp" | wc -l&lt;BR /&gt;&lt;BR /&gt;maybe use tr in there to convert SMTP to smtp?&lt;BR /&gt;&lt;BR /&gt;changes "smtp" into "smtp\n" so each smtp is on a separate line, then uses grep/wc to count them. Just a thought.</description>
      <pubDate>Mon, 26 Sep 2005 11:27:29 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634816#M105013</guid>
      <dc:creator>Stephen Keane</dc:creator>
      <dc:date>2005-09-26T11:27:29Z</dc:date>
    </item>
    <item>
      <title>Re: counting the number of times a word appears in a file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634817#M105014</link>
      <description>here is a program that might do a beter job&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://hpux.cs.utah.edu/hppd/hpux/Misc/conc-0.5/" target="_blank"&gt;http://hpux.cs.utah.edu/hppd/hpux/Misc/conc-0.5/&lt;/A&gt;</description>
      <pubDate>Mon, 26 Sep 2005 11:42:35 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634817#M105014</guid>
      <dc:creator>curt larson_1</dc:creator>
      <dc:date>2005-09-26T11:42:35Z</dc:date>
    </item>
    <item>
      <title>Re: counting the number of times a word appears in a file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634818#M105015</link>
      <description>Curt thanks for the two replies but no luck on the awk, I am not sure the correct writing format in a script. &lt;BR /&gt;&lt;BR /&gt;Test file with 3 lines&lt;BR /&gt;this is a test of the smtp; but the SMTP would %smtp smtp SMTP&amp;amp; work&lt;BR /&gt;helo smtp&lt;BR /&gt;&lt;BR /&gt;cat /tmp/test | tr "[:upper:]" "[:lower:]" | tr -cs "[a-z0-9]" "\012" |sort | uniq -c | sort +0nr +1d&lt;BR /&gt;+ cat /tmp/test&lt;BR /&gt;+ tr [:upper:] [:lower:]&lt;BR /&gt;+ tr -cs [a-z0-9] \012&lt;BR /&gt;+ sort&lt;BR /&gt;+ sort +0nr +1d&lt;BR /&gt;+ uniq -c&lt;BR /&gt;   1 helo smtp&lt;BR /&gt;   1 this is a test of the smtp; but the smtp&lt;BR /&gt;   1 would %smtp smtp smtp&amp;amp; work&lt;BR /&gt;&lt;BR /&gt;With your awk scrip i get the following error good ole bail out&lt;BR /&gt;&lt;BR /&gt;Tahoe: /tmp ./t2.sh&lt;BR /&gt;#!/bin/ksh -xv&lt;BR /&gt;cat /tmp/test |awk '{&lt;BR /&gt;for (i=1, i&amp;lt;=NF;i++) &lt;BR /&gt;num[$i]++;&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;END{&lt;BR /&gt;for ( word in num )&lt;BR /&gt;print word, num[word];&lt;BR /&gt;}'|grep -i smtp&lt;BR /&gt;+ cat /tmp/test&lt;BR /&gt;+ awk {&lt;BR /&gt;for (i=1, i&amp;lt;=NF;i++) &lt;BR /&gt;num[$i]++;&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;END{&lt;BR /&gt;for ( word in num )&lt;BR /&gt;print word, num[word];&lt;BR /&gt;}&lt;BR /&gt;+ grep -i smtp&lt;BR /&gt; syntax error The source line is 2.&lt;BR /&gt; The error context is&lt;BR /&gt;                for &amp;gt;&amp;gt;&amp;gt;  (i=1, &amp;lt;&amp;lt;&amp;lt; &lt;BR /&gt; awk: The statement cannot be correctly parsed.&lt;BR /&gt; The source line is 2.&lt;BR /&gt; syntax error The source line is 2.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 26 Sep 2005 11:43:59 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634818#M105015</guid>
      <dc:creator>Belinda Dermody</dc:creator>
      <dc:date>2005-09-26T11:43:59Z</dc:date>
    </item>
    <item>
      <title>Re: counting the number of times a word appears in a file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634819#M105016</link>
      <description>Hi James:&lt;BR /&gt;&lt;BR /&gt;Try this:&lt;BR /&gt;&lt;BR /&gt;# perl -lne '$count++ while (m/smtp/ig);END{print $count}' logfile&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Mon, 26 Sep 2005 11:46:26 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634819#M105016</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2005-09-26T11:46:26Z</dc:date>
    </item>
    <item>
      <title>Re: counting the number of times a word appears in a file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634820#M105017</link>
      <description>James, you have come through for me more times than I can count, thank you very much and thanks to the rest of you guys for and for all the help you provided.</description>
      <pubDate>Mon, 26 Sep 2005 11:53:56 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634820#M105017</guid>
      <dc:creator>Belinda Dermody</dc:creator>
      <dc:date>2005-09-26T11:53:56Z</dc:date>
    </item>
    <item>
      <title>Re: counting the number of times a word appears in a file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634821#M105018</link>
      <description>| tr -cs "[a-z0-9]" "\012"&lt;BR /&gt;&lt;BR /&gt;should be&lt;BR /&gt; | tr -cs "[a-z0-9]" "\012*"  &lt;BR /&gt;star after \012&lt;BR /&gt;&lt;BR /&gt;and &lt;BR /&gt;&lt;BR /&gt;The error context is&lt;BR /&gt;for &amp;gt;&amp;gt;&amp;gt; (i=1, &amp;lt;&amp;lt;&amp;lt; &lt;BR /&gt;awk: The statement cannot be correctly parsed.&lt;BR /&gt;i=1, &amp;lt; no comma, it is a semi colon ";"</description>
      <pubDate>Mon, 26 Sep 2005 11:54:08 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634821#M105018</guid>
      <dc:creator>curt larson_1</dc:creator>
      <dc:date>2005-09-26T11:54:08Z</dc:date>
    </item>
    <item>
      <title>Re: counting the number of times a word appears in a file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634822#M105019</link>
      <description>Curt, I got the awk statement to work, thanks, it gives me a count for each line, so I had to add a counter for that, still wouldn't give me a correct count for the tr usage.  But thanks again guys...</description>
      <pubDate>Mon, 26 Sep 2005 11:58:02 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634822#M105019</guid>
      <dc:creator>Belinda Dermody</dc:creator>
      <dc:date>2005-09-26T11:58:02Z</dc:date>
    </item>
    <item>
      <title>Re: counting the number of times a word appears in a file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634823#M105020</link>
      <description>Hi (again) James:&lt;BR /&gt;&lt;BR /&gt;To improve thins a bit, I'd amend my expression thusly:&lt;BR /&gt;&lt;BR /&gt;# perl -lne '$count++ while (m/\bsmtp\b/ig);END{print $count}' logfile&lt;BR /&gt;&lt;BR /&gt;This eliminates counting the string "smtp" (in any case or mixture) *within* the bounds of another string.  That is, this ould *not* count "this_was_from_smtp".&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 26 Sep 2005 12:11:20 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634823#M105020</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2005-09-26T12:11:20Z</dc:date>
    </item>
    <item>
      <title>Re: counting the number of times a word appears in a file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634824#M105021</link>
      <description>James,&lt;BR /&gt;&lt;BR /&gt;Within awk set your field separator to the word smtp or SMTP and then count its occurence within the input file as:&lt;BR /&gt;&lt;BR /&gt;# awk -F"smtp|SMTP" 'BEGIN{cnt=0} {cnt+=(NF-1)} END{print cnt}' input_file&lt;BR /&gt;&lt;BR /&gt;cheers!</description>
      <pubDate>Tue, 27 Sep 2005 02:43:13 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634824#M105021</guid>
      <dc:creator>Sandman!</dc:creator>
      <dc:date>2005-09-27T02:43:13Z</dc:date>
    </item>
    <item>
      <title>Re: counting the number of times a word appears in a file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634825#M105022</link>
      <description>In fact to make your "find &amp;amp;&amp;amp; count SMTP" case in-sensitive do the following:&lt;BR /&gt;&lt;BR /&gt;# awk -F"[sS][mM][tT][pP]" 'BEGIN{cnt=0} {cnt+=(NF-1)} END{print cnt}' input_file&lt;BR /&gt;&lt;BR /&gt;cheers!</description>
      <pubDate>Tue, 27 Sep 2005 05:21:20 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634825#M105022</guid>
      <dc:creator>Sandman!</dc:creator>
      <dc:date>2005-09-27T05:21:20Z</dc:date>
    </item>
    <item>
      <title>Re: counting the number of times a word appears in a file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634826#M105023</link>
      <description>Thanks Sandman, your awk version also works, I had to change the cnt from cnt=0 to cnt=1 to get the accurate count.</description>
      <pubDate>Tue, 27 Sep 2005 08:54:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/counting-the-number-of-times-a-word-appears-in-a-file/m-p/3634826#M105023</guid>
      <dc:creator>Belinda Dermody</dc:creator>
      <dc:date>2005-09-27T08:54:54Z</dc:date>
    </item>
  </channel>
</rss>

