<?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 Re: shell script problem in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/shell-script-problem/m-p/2555276#M886771</link>
    <description>Hi&lt;BR /&gt;Do you want to write in perl?&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;#!/usr/local/bin/perl&lt;BR /&gt;$filename=shift;  #first argument is filename&lt;BR /&gt;$pattern=shift;   #second argument is pattern&lt;BR /&gt;&lt;BR /&gt;open (FILE,"$filename") ||die "can't open";&lt;BR /&gt;&lt;BR /&gt;while (&lt;FILE&gt;)&lt;BR /&gt;{&lt;BR /&gt;  if(/$pattern/) &lt;BR /&gt;  {&lt;BR /&gt;    count++;  #put your logic here&lt;BR /&gt;   }&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;This will get you started.&lt;BR /&gt;&lt;BR /&gt;Sachin&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/FILE&gt;</description>
    <pubDate>Fri, 20 Jul 2001 18:23:57 GMT</pubDate>
    <dc:creator>Sachin Patel</dc:creator>
    <dc:date>2001-07-20T18:23:57Z</dc:date>
    <item>
      <title>shell script problem</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/shell-script-problem/m-p/2555273#M886768</link>
      <description>I was wondering how to write a script that accepts a pattern and filename as arguments and then counts the number of occurrences of the pattern in the file (A pattern may occur more than once in a line and comprise only alphanumeric characters and underscore).  &lt;BR /&gt;&lt;BR /&gt;Any assistance would be appreciated!!&lt;BR /&gt;&lt;BR /&gt;Arrivederci ...</description>
      <pubDate>Fri, 20 Jul 2001 17:18:08 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/shell-script-problem/m-p/2555273#M886768</guid>
      <dc:creator>Landen</dc:creator>
      <dc:date>2001-07-20T17:18:08Z</dc:date>
    </item>
    <item>
      <title>Re: shell script problem</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/shell-script-problem/m-p/2555274#M886769</link>
      <description>you could do something like&lt;BR /&gt;grep $1 $2 | wc -l</description>
      <pubDate>Fri, 20 Jul 2001 17:20:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/shell-script-problem/m-p/2555274#M886769</guid>
      <dc:creator>Kevin Wright</dc:creator>
      <dc:date>2001-07-20T17:20:54Z</dc:date>
    </item>
    <item>
      <title>Re: shell script problem</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/shell-script-problem/m-p/2555275#M886770</link>
      <description>grep -c pattern filename</description>
      <pubDate>Fri, 20 Jul 2001 17:36:04 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/shell-script-problem/m-p/2555275#M886770</guid>
      <dc:creator>Charles McCary</dc:creator>
      <dc:date>2001-07-20T17:36:04Z</dc:date>
    </item>
    <item>
      <title>Re: shell script problem</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/shell-script-problem/m-p/2555276#M886771</link>
      <description>Hi&lt;BR /&gt;Do you want to write in perl?&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;#!/usr/local/bin/perl&lt;BR /&gt;$filename=shift;  #first argument is filename&lt;BR /&gt;$pattern=shift;   #second argument is pattern&lt;BR /&gt;&lt;BR /&gt;open (FILE,"$filename") ||die "can't open";&lt;BR /&gt;&lt;BR /&gt;while (&lt;FILE&gt;)&lt;BR /&gt;{&lt;BR /&gt;  if(/$pattern/) &lt;BR /&gt;  {&lt;BR /&gt;    count++;  #put your logic here&lt;BR /&gt;   }&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;This will get you started.&lt;BR /&gt;&lt;BR /&gt;Sachin&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/FILE&gt;</description>
      <pubDate>Fri, 20 Jul 2001 18:23:57 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/shell-script-problem/m-p/2555276#M886771</guid>
      <dc:creator>Sachin Patel</dc:creator>
      <dc:date>2001-07-20T18:23:57Z</dc:date>
    </item>
    <item>
      <title>Re: shell script problem</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/shell-script-problem/m-p/2555277#M886772</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;This is a fairly interesting one in that you have to exemine each line for possible multiple matches. My solution is to call awk as a 'here doc' and use the gsub function to substitute the target string with a nonsense string. gsub returns the number of substitutions and hence the number of pattern matches. We dont save the altered line so no harm is done.&lt;BR /&gt;&lt;BR /&gt;Usage: count.sh target file1 file2 ...&lt;BR /&gt;will list each file and the count of matches&lt;BR /&gt;or count.sh target &amp;lt; stdin will read stdin and count the number of pattern matches.&lt;BR /&gt;&lt;BR /&gt;Enjoy, Clay</description>
      <pubDate>Fri, 20 Jul 2001 18:28:44 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/shell-script-problem/m-p/2555277#M886772</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2001-07-20T18:28:44Z</dc:date>
    </item>
    <item>
      <title>Re: shell script problem</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/shell-script-problem/m-p/2555278#M886773</link>
      <description>here is a script from Unix power tools by O'reilly.  I'm sure you can adapt it to your situation.  Basically, do tr to put each word on a separate line, then count the lines.  something like:&lt;BR /&gt;&lt;BR /&gt;cat your_file | tr -cs "[:alnum:]_" "[\012*]" | grep -c your_pattern&lt;BR /&gt;&lt;BR /&gt;#! /bin/sh&lt;BR /&gt;### wordfreq - count number of occurrences of each word in input&lt;BR /&gt;### Usage: wordfreq [-i] [files]&lt;BR /&gt;#&lt;BR /&gt;# ** CONFIGURATION NOTE **: See comments above second "tr" command below&lt;BR /&gt;#&lt;BR /&gt;## wordfreq counts the number of occurrences of each word in its input.&lt;BR /&gt;## If you give it files, it reads from them; otherwise it reads stdin.&lt;BR /&gt;## The -i option folds upper case into lower case (capitalized letters&lt;BR /&gt;## will count the same as lower-case).&lt;BR /&gt;#&lt;BR /&gt;# Adapted from "concordance", which Carl Brandauer posted to USENET.&lt;BR /&gt;&lt;BR /&gt;# Different versions are a pain... :-(&lt;BR /&gt;case "$1" in&lt;BR /&gt;-i) shift&lt;BR /&gt; tr1="[a-z]"&lt;BR /&gt; tr2bsd="a-z'" tr2sys5="[a-z]'"&lt;BR /&gt; ;;&lt;BR /&gt;*) # no case conversion&lt;BR /&gt; tr1="[A-Z]"&lt;BR /&gt; tr2bsd="A-Za-z'" tr2sys5="[A-Z][a-z]'"&lt;BR /&gt; ;;&lt;BR /&gt;esac&lt;BR /&gt;&lt;BR /&gt;cat ${1+"$@"} |   # Work around problem with "$@" in some shells&lt;BR /&gt;tr "[A-Z]" "$tr1" |  # Convert upper case to lower if -i option&lt;BR /&gt;#&lt;BR /&gt;# NOTE: If you use Berkeley tr(1), comment out the second tr command and&lt;BR /&gt;# uncomment the first tr command:&lt;BR /&gt;#&lt;BR /&gt;#tr -cs "$tr2bsd" "\012" |&lt;BR /&gt;tr -cs "$tr2sys5" "[\012*]" | # Replace all characters not a-z or ' with&lt;BR /&gt;    # a new line. i.e. one word per line&lt;BR /&gt;sort |    # uniq expects sorted input&lt;BR /&gt;uniq -c |   # Count the number of times each word appears&lt;BR /&gt;sort +0nr +1d   # Sort first from most to least frequent,&lt;BR /&gt;    # then alphabetically&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 20 Jul 2001 19:37:16 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/shell-script-problem/m-p/2555278#M886773</guid>
      <dc:creator>Curtis Larson_1</dc:creator>
      <dc:date>2001-07-20T19:37:16Z</dc:date>
    </item>
    <item>
      <title>Re: shell script problem</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/shell-script-problem/m-p/2555279#M886774</link>
      <description>Hi:&lt;BR /&gt;&lt;BR /&gt;Very interesting problem!  Here's a small, unembellished script which returns the count of the matched pattern.&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;typeset P=$1&lt;BR /&gt;typeset F=$2&lt;BR /&gt;R=`awk -v P=$P '{for (i=1;i &amp;lt;=NF;i++) ary [$i]=1}&lt;BR /&gt;  END{ for (S in ary) if (S~P) {k=k+1};print k}' $F`&lt;BR /&gt;echo $R&lt;BR /&gt;#_end.&lt;BR /&gt;&lt;BR /&gt;Call the script "my.sh" and execute this:&lt;BR /&gt;&lt;BR /&gt;# ./my.sh local /etc/hosts&lt;BR /&gt;&lt;BR /&gt;...This will print "1" for having found the string "localhost" in /etc/hosts.&lt;BR /&gt;&lt;BR /&gt;# ./my.sh lo /etc/hosts&lt;BR /&gt;&lt;BR /&gt;...will print "3" since "lo" matched "loghost" on one line, and both "localhost" and "loopback" together on another line.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Fri, 20 Jul 2001 21:48:41 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/shell-script-problem/m-p/2555279#M886774</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2001-07-20T21:48:41Z</dc:date>
    </item>
    <item>
      <title>Re: shell script problem</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/shell-script-problem/m-p/2555280#M886775</link>
      <description>Hi James,&lt;BR /&gt;&lt;BR /&gt;Nice solution. We probably should suggest that both of our scripts should actually feed awk with a grep command. Grep's generally faster and then awk would only need to look for the matched lines for multiple patterns.&lt;BR /&gt;&lt;BR /&gt;Regards, Clay</description>
      <pubDate>Fri, 20 Jul 2001 22:18:52 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/shell-script-problem/m-p/2555280#M886775</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2001-07-20T22:18:52Z</dc:date>
    </item>
    <item>
      <title>Re: shell script problem</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/shell-script-problem/m-p/2555281#M886776</link>
      <description>Hi Clay:&lt;BR /&gt;&lt;BR /&gt;Thanks.  Your suggestion to use 'grep' to do the initial filtering is a nice touch.  In my script's case, the array built for subsequent evaluation would be greatly reduced in size.  I find that intrinsically appealing having grown up in environments where it was cheaper to invest programming time to gain performance than to "throw more hardware" at the problem.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Sat, 21 Jul 2001 23:04:13 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/shell-script-problem/m-p/2555281#M886776</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2001-07-21T23:04:13Z</dc:date>
    </item>
  </channel>
</rss>

