RPGIV @ Work

A unique site for RPG and System i Lovers


Hi, this site will provide all what you need in System i and RPG developments.

My Name is Chamara Withanachchi, System i Expert and RPG Developer. And in the field for last 11 years.

I hope you will find lot of valuable information from this site

Grep Print E-mail
User Rating: / 0
Written by Chamara   


Grep has the following form:

grep [options] regular-expression [input-files]

The brackets around the options and input-files parameters to indicate that these two parameters are optional. You will probably specify options sometimes and not specify options other times, but you will probably use the input files parameter more often than not.

The regular-expression parameter is the string for which you are searching. The simplest form of regular expression is an exact sequence of characters for which the system is to search.


If you omit the input-files parameter, grep reads from the standard input device, which by default is the keyboard. This may seem goofy, but it's in keeping with the way other Unix utilities operate. Since you will not need grep to find strings in input that you type yourself, you are not likely to omit the input-files parameter when running grep from the keyboard under Qshell.

The only time you are likely to omit the input-files parameter is when grep is reading the output of another command through a pipeline. In the following example, output of the List Directory Contents (ls) command is the input to grep:

ls   |   grep   -i   '[A-Z][12]' 

The vertical bar creates the pipeline. This example lists files whose names contain a letter of the alphabet, followed by either a one or a two. (It will make more sense to you when you read about regular expressions, below.)

There are several different ways to fill in the input-files parameter. One way is to list a file name, like this:

grep '22.34'  mydata.csv

In this case, only one file (mydata.csv) is searched for in the current directory. You can specify a full path on the file name, of course:

grep '22.34'  /home/jsmith/mydata.csv

You may want to use wildcard characters to search more than one file. The following command searches all the .csv files in the current directory:

grep   '22.34'  *.csv

In these two examples, the input-files parameter has only one argument. You can list more than one file, if you wish. Separate them with white space. This command searches three files:

grep '22.34' fileone.csv filetwo.csv filethree.csv

All of these example search IFS files, but you can search source physical file members, too. The following command searches all members of MYLIB/MYSRC for the string pgm:

grep   'pgm'   /qsys.lib/mylib.lib/src.file/*.*

This command searches all members of a source physical file, as well as all HTML and text files in the current IFS directory:

grep 'pgm' /qsys.lib/js.lib/src.file/*.*  *.htm*  *.txt

One comment about output files: By default, output goes to the display. If you want to send the output to a file, use a redirection operator to send it there. The output of this grep command goes to an IFS file called grep2.out.

grep     '22.34'    *.csv    >/home/jsmith/grep2.out

Here's an example of directing grep output to a source physical file member:

grep  -i  'class'  *.java  >/qsys.lib/js.lib/src.file/one.mbr

Simple Searches

Now that you know the syntax of grep, look at a few simple searches:

grep   int   *.java


grep   'int'   *.java


grep   "int"   *.java

The grep command, in either of these three forms, instructs the system to search all files with a .java extension in the current directory for the string int. Output might look something like this:

J1.java:        System.out.println("\nThis is class J1\n");
J2.java:        System.out.println("\nThis is class J2\n"); 
Mkc.java:        System.out.println("  file = LF"); 
Mkc.java:        ex.printStackTrace(); 
Mkc.java:    int count = 0; 
Mkc.java:            for (int i=1; i<=numCols; i++) {

Do you see int somewhere in each line? In some lines, int is a word unto itself, while in others int is part of another word, such as println.

Output goes to the display, but you can redirect it to an IFS file instead, as in the following example:

grep  'int'   *.java    >grepout.data

Grep produces the same output but writes the records to file grepout.data.

Notice that the search string may or may not be surrounded by apostrophes or quotation marks. If you omit the apostrophes or quotation marks, Qshell will try to replace certain characters with other values. For example, it will replace a tilde (~) with the name of the current working directory. For that reason, it is a good idea to use either apostrophes or quotation marks as a rule and omit them only when you want Qshell to expand special values. Be aware that some implementations of grep will accept quotation marks but not apostrophes.


These are the options grep supports under Qshell (you may find other options in grep on other platforms):

Option Description
-c write count of selected lines
-e use a list of search patterns
-f use a file of search patterns
-h do not print filename headers
-i or -y ignore case
-l write names of files
-n show line numbers
-q do not print messages (quiet mode)
-s suppress error messages for nonexistent or unreadable files
-v write lines that do not match patterns
-w search as whole word
-x Match if pattern is the only thing on the line
-E Use Extended Regular Expressions (EREs)
-F Do not recognize regular expressions

Here are examples of how some of these work.

Suppose you want to find declarations of integer variables in Java programs but no print commands. Use the–w option to tell grep to find int when it is a stand-alone word, not part of another word..

grep   –w   'int'   *.java

Grep responds with the following:

Mkc.java:    int count = 0; 
Mkc.java:            for (int i=1; i<=numCols; i++) {

Maybe you'd like to know where those lines are found in the source files. If so, add the –n option to ask grep to show line numbers:

grep  -w   -n  'int'   *.java


grep  -wn   'int'   *.java

Note that you may specify the options separately or combine them behind one hyphen. Grep responds with the following:

Mkc.java:45:    int count = 0; 
Mkc.java:92:            for (int i=1; i<=numCols; i++) {

The line number follows the file name and precedes the line image.

To get all lines that don't fit the pattern, use the -v option:

grep   -v   'int'   *.java

Grep lists the lines of Java source code that do not contain int. To remember this option, think of the word invert.

Two other Qshell commands incorporate some of these options. The egrep command is equivalent to grep with the –E option. The fgrep command is equivalent to grep with the –F option.

Regular Expressions

If you're going to do any work at all in a Unix-like environment--and that includes Qshell--you need to familiarize yourself with regular expressions. Regular expressions are used in various languages and utilities, including Perl, Tcl, sed, Python, and awk, as well as within various text editors.

A regular expression describes a search string. It may contain characters that are to be matched exactly, as when searched for int above. But a regular expression may also contain symbols that tell what types of characters are to be sought. For example, [0-9] means look for any character between 0 and 9, inclusive.

The following table describes the special symbols and their meanings:

Symbol Meaning
. (period) Matches any character except end of line
* Matches zero or more occurrences of the preceding character
^ Matches beginning of line
$ Matches end of line
[] Matches any character within brackets; ranges may be specified with hyphen
\{x\} Matches exactly x occurrences of the preceding character
\{x,\} Matches x or more occurrences of the preceding character
\{x,y\} Matches x to y occurrences of the preceding character
\ Turns off special meaning of following character 

To illustrate how regular expressions work, here are some grep commands within Qshell, along with explanations of what each one accomplishes:

Expression Meaning
'b.b' Lines containing two b's separated by any character
'<script' Lines containing <script
'</*script' Lines containing <script or </script
'[a-z][0-9]' Lines containing a lowercase letter followed by a digit
'[a-z|A-Z][0-9]'  Lines containing a letter (in either case) followed by a digit
'.com' Lines containing any character followed by com
'\.com' Lines containing a period followed by com
'3\{2\}' Lines containing exactly two 3's together

You may run into a few limitations when using grep under Qshell. For starters, you may not be able to get the caret (^) symbol to work.

Here are some regular expressions that couldn't get to work under Qshell but worked fine under grep on PC:

Expression Meaning
'</p>$' Lines that end with an HTML paragraph tag (</p>)
'^a' Lines that begin with the letter a
'[^g-i]x' Lines with an x preceded by any letter except g, h, or i

There is no shortage of information about regular expressions on the Web. Here are two links to get you started (a search engine will turn up many more):

  • http://etext.lib.virginia.edu/helpsheets/regex.html
  • www.netaxs.com/people/nerp/automata/regexp0.html
  • Keep in mind that some of the special characters you come across may not be supported by Qshell's grep. If you run into this issue, the best thing to do is to key the special characters into a Word document, or some other PC file, and copy and paste it to your Qshell environment. It may seem a bit hokey, but it works.

    User Comments

    Please login or register to add comments