Sponsored Content
Top Forums Shell Programming and Scripting How to count number of results found? Post 302770871 by gary_w on Monday 18th of February 2013 05:54:54 PM
Old 02-18-2013
Code:
#!/bin/ksh

FILE1=x1.dat
FILE2=x2.dat

while read word
do
  # -w option to grep for searching on a word boundary.
  count=$( grep -wc "$word" $FILE2 )
  printf "%s: %d\n" "$word" $count
done < $FILE1

exit 0

Code:
$ cat x1.dat
word1
word2
word3
$ cat x2.dat
word1
word2
word3
word1
word2
word3oword1
word2
word2
word2
word3
word3
word3
word3 word3word3
$ ./x
word1: 2
word2: 5
word3: 5
$

I would advise against using tr, since that translation of the entire file is happening for each word in FILE1.

Last edited by gary_w; 02-18-2013 at 07:02 PM..
This User Gave Thanks to gary_w For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

awk | stop after specified number of results

I am searching some rather large text files using grep and or awk. What I would like to know is if there is a way (either with grep, awk, or realy any other unix tool) to stop the search when a predifined number of results are returned. I would like to do this for speed purpuses. When i get... (6 Replies)
Discussion started by: evan108
6 Replies

2. Shell Programming and Scripting

Number count per number ranges

Hi, I have a question here that need to get advise from all of you. Let say I have a set of data 12347777 12359899 12347677 12360090 12347688 12359979 12359009 12367022 12346677 I need to count the number that appear in each numbering ranges and the output is like below: Prefix ... (5 Replies)
Discussion started by: shirleyeow
5 Replies

3. UNIX for Dummies Questions & Answers

putting grep -c results number in a variable

I want to display "no results found" if a grep search of a name that the user inputs is not found anywhere in a certain file, Right now I have this, but doesn't seem to work. Im not sure what to change. read name results=grep -c $name file if ; then echo "No results found." exit... (1 Reply)
Discussion started by: busdude
1 Replies

4. Shell Programming and Scripting

count the number of lines that start with the number

I have a file with contents similar to this. abcd 1234 4567 7666 jdjdjd 89289 9382 92 jksdj 9823 298 I want to write a shell script which count the number of lines that start with the number (disregard the lines starting with alphabets) (1 Reply)
Discussion started by: grajp002
1 Replies

5. Shell Programming and Scripting

found count of them

Hi guys i must write a program which read logfile of my program and print the result. i wrote this and i want print of the user who have virus. i extract user and put it in file, now i want know each user have how many virus. how can i do this in bash, my file is like: and soso i... (5 Replies)
Discussion started by: Skipper
5 Replies

6. Shell Programming and Scripting

Awk - Count instances of a number in col1 and put results in a col2 (new) of diff file

I have 2 files as follows: filename1: : 6742 /welcome/mundial98_ahf1_404.htm 1020 6743 /welcome/mundial98_ahf1_404.htm 2224 6744 /welcome/mundial_ef1_404.htm 21678 6745 /welcome/mundial_if_404.htm 4236 6746 /welcome/mundial_lf1_404.htm 21678 filename2: 6746 894694763 1... (2 Replies)
Discussion started by: jontjioe
2 Replies

7. Shell Programming and Scripting

how to add the number of row and count number of rows

Hi experts a have a very large file and I need to add two columns: the first one numbering the incidence of records and the another with the total count The input file: 21 2341 A 21 2341 A 21 2341 A 21 2341 C 21 2341 C 21 2341 C 21 2341 C 21 4567 A 21 4567 A 21 4567 C ... (6 Replies)
Discussion started by: juelillo
6 Replies

8. Shell Programming and Scripting

awk Help -- If match found return the count

Hi All, I need to get the count of records in the file, if the passing parameter matches with the list of records in the file. Below is my example source file: Test1.dat 20120913 20120913 20120912 20120912 20120912 20120912 20120912 20120913 20120913 20120912 In my script I am... (5 Replies)
Discussion started by: bbc17484
5 Replies

9. Shell Programming and Scripting

Count occurences of a character in a file by sorting results

Hello, I try to sort results of occurences in an array by using awk but I can't find the right command. that's why I'm asking your help ! :) Please see below the command that I run: awk '{ for ( i=1; i<=length; i++ ) arr++ }END{ for ( i in arr ) { print i, arr } }' dictionnary.txt ... (3 Replies)
Discussion started by: destin45
3 Replies

10. UNIX for Beginners Questions & Answers

Grep command to show the number of results

Hi I wanted to know if there is an option in grep command to show the number of results (not the number of lines of findings). Thanks (14 Replies)
Discussion started by: abdossamad2003
14 Replies
man.repro(7)						 Miscellaneous Information Manual					      man.repro(7)

Name
       man.repro - the man.repro macro package for typesetting reference pages

Syntax
       tbl file...  | nroff [ -nN ] [ -rpS ] [ -rl1 ] -man.repro | col | ...
       tbl file...  | *troff [ -nN ] [ -rpS ] [ -rl1 ] -man.repro | ...

Description
       The macro package is used to format reference manual pages for printing or typsetting.  This reference page was formatted by command, using
       the macro package, or was formatted by the and the commands, using the macro package.

       The page size is 80 columns by 66 lines for output and is 8.5" x 11" when formatted with text formatters.  Page numbers appear at the  bot-
       tom  of	each output page with odd page numbers appearing on the right side and even page numbers appearing on the left side.  Page footers
       can optionally include the name of the reference page section.

       The format of the ULTRIX online reference pages is determined by the macro package.

   Macros
       The following describes the macros in the macro package.

       Any text argument can range from zero to six words.  Quotation marks (" ") can be used to include blanks in words.  If text is  not  speci-
       fied,  special treatment is applied to the next input line that has text to be printed.	In this way, can be used to italicize a whole line
       or followed by to make small bold letters.

       A prevailing indent distance is remembered between successive indented paragraphs, and is reset to a default value upon reaching  a  nonin-
       dented paragraph.  Default units for indents i are ens (an en is 1 character or 1/2 em space in current point size).

       Typeface and size are reset to default values before each paragraph, and after processing font and size setting macros.

       .B [ text... ]
		   Sets text text in boldface.	If no text is specified, the next text line is set in boldface.

       .BI word1 word2 [ words... ]
		   Sets word1 in boldface, word2 in an italic typeface, and then alternates between these two fonts for the remaining words, up to
		   six words.  Blanks between words are stripped unless the string is enclosed in quotation marks (" ").

       .BR word1 word2 [ words... ]
		   Sets word1 in boldface, word2 in a roman typeface, and then alternates between these two fonts for the remaining words,  up	to
		   six words.  Blanks between words are stripped unless the string is enclosed in quotation marks (" ").

       .CT character
		   Prints the keyboard control character indicator . For example, prints as .

       .CW	   Sets text in a constant width font until another font change is encountered.

       .De	   Ends an unfilled display block (started by Also ends automatic centering, if it was in effect.

       .Ds	   Starts  an unfilled display block.  Text between and is printed in a roman typeface, with `no fill' mode (no wrapping and blank
		   lines allowed) in effect.  The display block is set flush left.

       .DT	   Restores default tabs.  Default tabs are set to .5 inches, starting with .5i, 1i, ... .

       .EE	   Ends an example and restores basic text defaults and indents.

       .EX [ i ]   Starts an example.  Text between and is printed in a constant width font with `no fill'  mode  (no  wrapping  and  blank  lines
		   allowed) in effect.	The example is set flush left unless an indent i is specified.	Units of i are ens.

       .G [ text... ]
		   Sets text in a sans-serif typeface.	If no text is specified, the next text line is set in a sans-serif typeface.

       .GL [ text... ]
		   Sets text in a sans-serif italic typeface.  If no text is specified, the next text line is set in a sans-serif italic typeface.

       .HB [ words... ]
		   Sets  the  text in underline mode or in a sans-serif bold typeface, depending on the type of text formatter or If the text for-
		   matter is of type the next 999 input lines are formatted in underline mode italic mode), or all the lines up to a  font  change
		   are	formatted  in underline mode, depending on which limit is encountered first.  If the text formatter is of type text is set
		   in a sans-serif bold typeface until a font change is encountered.  Up to nine words can also be specified as arguments.

       .HP [i]	   Begins a paragraph with a hanging indent of i ens.

       .I [ text... ]
		   Sets text in an italic typeface.  If no text is specified, the next text line is set in an italic typeface.

       .I1 word    Sets a temporary indent to the length of the specified word.

       .I2 word    Reverses one line and then sets a temporary indent to the length of the specified word.

       .IB word1 word2 [ words... ]
		   Sets word1 in an italic typeface, word2 in boldface, and then alternates between these two fonts for the remaining words, up to
		   six words.  Blanks between words are stripped unless the string is enclosed in quotation marks (" ").

       .IP x [i]   Sets  the  prevailing  indent to i.	Then begins the indented paragraph with a hanging tag given by the next text line.  If the
		   tag does not fit, the macro places the next text on a separate line.  Tag x appears in bold typeface.

       .IR word1 word2 [ words... ]
		   Sets word1 in an italic typeface, word2 in a roman typeface, and then alternates between these  two	fonts  for  the  remaining
		   words, up to six words.  Blanks between words are stripped unless the string is enclosed in quotation marks (" ").

       .LP	   Same as the macro.  This macro is obsolete, but is provided for backwards compatibility.

       .MS reference_page section_subsection [ punctuation ]
		   Sets  reference_page  immediately  followed	by section_subsection in parentheses followed by optional punctuation, using fonts
		   that distinguish this reference page reference from ordinary text.  For example,

       .NE	   Ends a note. Also cancels automatic centering if it was in effect.

       .NT [ header1 ] [ C ]
       .NT [ C ] [ header2 ]
		   Starts a note.  If no arguments are specified, the default header for the note is `Note'.  If the first argument is the  letter
		   `C',  all text in the note is centered, for the next 99 text lines or until the macro is called, whichever comes first.  If the
		   first argument is not `C', it becomes the header of the note, even if header2 is also specified.  The header2 argument  becomes
		   the header of the note if the first argument is `C'.

       .PD [ v ]   Sets the interparagraph distance to v vertical spaces.  Resets the distance to the default value if v is omitted.

       .PN x [ y ] Sets  x  in	an  italic or constant width typeface (depending on the formatter type) and then reverts to the previous typeface.
		   The optional argument y is appended to x with no space, but printed in the previous typeface.  The x argument is usually a path
		   name; y is usually punctuation.

       .Pn x y [ z ]
		   Sets x in the current typeface, sets y in an italic or constant width typeface (depending on the formatter type) and appends it
		   to x, and finally reverts to the previous typeface.	The optional argument z is appended to y,  but	printed  in  the  previous
		   typeface.  Spaces are removed between x, y, and z, unless quotation marks (" ") are used to enclose strings with spaces.  The x
		   argument is usually a fixed path name; y is usually a variable path name; and z is usually punctuation.

       .PP	   Starts a block paragraph.  Sets the prevailing indent to .5i for and four picas for text formatters.

       .R	   Sets the text in a roman typeface until another font change is encountered.	Also ends underline mode if it was in effect.

       .RB word1 word2 [ words... ]
		   Sets word1 in a roman typeface, word2 in boldface, and then alternates between these two fonts for the remaining words,  up	to
		   six words.  Blanks between words are stripped unless the string is enclosed in quotation marks (" ").

       .RE [ k ]   Returns to the kth relative right shift indent level.  (Restores the left margin to the position prior to the kth call).  Spec-
		   ifying k=0 is equivalent to specifying k=1.	If k is omitted, restores the left margin to the most  recent  previous  position.
		   When k=1 or 0, the default indent increment is restored.

       .RI word1 word2 [ words... ]
		   Sets  word1	in  a  roman  typeface, word2 in an italic typeface, and then alternates between these two fonts for the remaining
		   words, up to six words.  Blanks between words are stripped unless the string is enclosed in quotation marks (" ").

       .RN	   Prints the return character indicator, .

       .RS [ i ]   Shifts the left margin to the right (relatively) the amount of i ens. The macro calls can be nested up to nine levels.  If i is
		   not	specified  for	the first call, the relative right shift increases .5 inch for and four picas for text formatters.  Nested
		   calls increment the relative indent by i ens, or by .2 inch for or by 2 picas for text formatters.

       .SH text    Creates a section header.

       .SM [ text ]
		   Sets text to be two points smaller than the current point size.  If no text is specified, the next text  line  is  set  in  the
		   smaller point size.

       .SS text    Creates a subsection header.

       .TB [ words... ]
		   Same as the macro.  This macro is obsolete, but is provided for backwards compatibility.

       .TH n c[s] [ a ] [ f ] [ x ]
		   Begins  a  new reference page and sets the page title.  Also sets up headers and footers for output pages, sets up all defaults
		   and traps, and calls the and macros.  The title appears as a header on all pages of the formatted reference page. The  n  argu-
		   ment is the reference page name.  The c argument is the primary section number or letter.  The s argument is the subsection, if
		   any.  The a argument is for an optional machine architecture specific label; for example ``VAX''.  The  f  argument	optionally
		   alters a portion of the page footer.  The x argument is for optional extra commentary; for example ``Unsupported''.

		   Fields  n,  c,  and	s  appear together at the top of each output page (see the top of this page for an example).  These fields
		   alternate between the right top and left top of a page header, corresponding to odd and even page  numbers.	 Field	a  appears
		   opposing the page name in the header when formatted with but appears as a bleed tab when formatted with text formatters.  The f
		   argument appears in the page footer on the inside edge of the page (left for odd page numbers, right for even).  The x argument
		   appears underneath the page name in the header.

		   The last three fields are optional.	To skip a field, specify a pair of quotation marks ("") in the field to be skipped.

       .TP [i]	   Sets  the  prevailing  indent to i.	Then begins the indented paragraph with a hanging tag given by the next text line.  If the
		   tag does not fit, the macro places the next text on a separate line.

       .UF footer  Replaces the section name (adjacent to the `chapter-page_number' pair in the page footer), defined by the option, with the text
		   footer.  This macro must not be called before the macro.

       .VE	   End a vertical margin bar.

       .VS [ 4 ]   Starts a vertical margin bar, if `4' is specified; otherwise, the macro does nothing.

   Macros That Cause Line Breaks
       The following macros cause line breaks:

	      De   Ds	EE   EX   HP   IP
	      LP   PP	RE   SH   SS   TH
	      TP

   Macros That Need Text Lines
       The following macros affect the following line of text if they are specified in the input without arguments:

	      B    BI	BR   G	  GL   I
	      IB   IR	RI   RB   SH   SS
	      SM

	  Defaults
	      Automatic hyphenation is turned off.

       Characters printed from the Special Font are artificially bolded by three units whenever the current font is `3'.

       The  default page size is 80 columns by 66 lines for output and 8.5" x 11" for output generated by text formatters.  The text area is hori-
       zontally placed on the page so that the effective page margin is .3 inches for and 7.5 picas for text formatters.

       The macro sets up the following defaults:

       o   Text is set in ``noadjust'' mode; the right margin is ragged.

       o   The default interparagraph distance is 1v for and .5v for text formatters.

       o   The basic text indent is .5 inches for and four picas for text formatters, from the left margin.

       o   The maximum text line length is 7.4 inches for and 36 picas for text formatters.

       o   Sets tab stops every .5 inches.

       o   The basic text point size is 11 points, with line spacing set to 12 points.

       o   The basic text font is ``R'' (a roman typeface).

       o   Reference page headers, section headers, and subsection headers are set in a sans-serif bold typeface.

Options
       -nN	   Numbers the first generated page as N.

       -rl1	   Turns on line double-spacing mode.

       -rpS	   Sets the section number to S.  The section number determines if the name of a section will appear in the page footer.   If  the
		   value  of S is 0, no name appears in the page footer.  When S is specified, that number determines the name that will appear in
		   the footer.	The section number appears in output page footers as S-N (chapter-page-number).  If S is not 0, the  name  of  the
		   section appears on the inside of the page footer, adjacent to the chapter-page-number sequence.

Restrictions
   Predefined Registers
       The following registers are predefined by the macro package and should not be changed:

       PO	   Page offset and page margin

       IN	   Left margin indent relative to the section headers

       LL	   Line length including

       PL	   Page length

       The register `l' is predefined when you specify the option. Its default value is 0.  The command does not use this option.

       The register `p' is predefined when you specify the option. Its default value is 0.  The command does not use this option.

       The register `p' should be set to a range 1-8 or 11-18 for unsupported reference pages.	It cannot be set to values `l', `n', `o', or `p'.

   Reserved Registers
       The following registers are reserved for internal use by the and macro packages:

	      A1   DX	EX   l	 p   p#
	      PF

       In addition, registers beginning with the characters `)', `]', and `}' are also reserved for internal use.

       Registers predefined by the commands, and the and text preprocessors and formatters should not be redefined.

   Predefined Strings
       The following strings are predefined by the macro package and should not be changed:

       lq	   " if `` if

       rq	   " if '' if

       S	   Command string to change type size to 10 points.

   Reserved Strings and Macros
       The following string and macro names are reserved for internal use by the and macro packages:

	      ##   A1	BD   BK   CD   D
	      DE   DS	HH   ID   LD   NO
	      NX   P	ya   yn   yl   ys

       In addition, names beginning with the characters `)', `]', and `}' are also reserved for internal use.

       Names predefined by the commands, and the and text preprocessors and formatters should not be redefined.

   .TH Macro Restrictions
       The section number should only be 1-8, `n', `l', `o', or `p'.  Other values might not be recognized by the or commands.

       Sections 6, 7, `n', `l', `o', and `p' do not currently have subsections, so subsections should not be specified.

       The  architecture field (a) should not exceed four characters.  A value longer than four characters might print outside the right page mar-
       gin.

       Reference pages containing commands should be preprocessed by an text preprocessor before being installed on the system.

       Reference pages containing commands must not be preprocessed before being installed on the system.

   The Name Section
       The command assumes the Name section of a reference page has the following format:
       name[, name, name ...] - explanatory text
       There should be at least one space after any comma and only one space following the ``backslash hyphen'' (-).  There  should  not  be  any
       commands in the explanatory text.  The explanatory text should be brief.  The command combines information in the Name section with parame-
       ters of the macro to create an entry in a database searched by the and commands.

Portability Considerations
       The ULTRIX macro packages contain extensions and enhancements borrowed from other macro packages.  If you have a  need  to  write  portable
       reference pages, you should not use the following macros:

	      CT   CW	De   Ds   EE   EX
	      G    GL	HB   I1   I2   LP
	      MS   NE	NT   PN   Pn   R
	      RN   TB	UF

       The and macros are obsolete.

       The  ULTRIX  macro  differs  from  other implementations of the macro.  The primary differences are in the placement of the page title, and
       third and fifth fields in the output.  The page title (the page name and section number) is commonly placed  on	both  sides  of  the  page
       header in other implementations.  The more common placement of the third field is in the center of the page footer.  The more common place-
       ment of the fifth field is in the center of the page header.

       The macro permits the use of the percent (%) character in any of its fields.  The presence of the percent character may cause problems  for
       other implementations of this macro.

       Use  of the and commands should be avoided, because the version of the command in some other implementations might not preprocess reference
       pages through the command.  The commands also might not be installed.

Examples
       The following example processes this manual page for a character-cell device:
       % cd /usr/man/man7
       % tbl man.repro.7 | nroff -rp7 -n5 -man.repro | col | lpr -Plp
       In this example, the option for initializes number register to specify that this page is from Section 7 of the Reference Pages.	The option
       specifies a starting page number of 5. The first page printed is numbered `7-5'.

Files
       The				macro package file

See Also
       col(1), man(1), nroff(1), tbl(1), man(7), man.nopage(7), catman(8)

																      man.repro(7)
All times are GMT -4. The time now is 01:33 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy