UNIX Hints & Hacks

ContentsIndex

Chapter 6: File Management

 

Previous ChapterNext Chapter

Sections in this Chapter:

   

6.1 Copy Files with Permissions and Time Stamps

 

6.5 Finding Files with grep

 

6.8 Moving and Renaming Groups of Files

 

6.11 Splitting Files

6.2 Copy Files Remotely

 

6.6 Multiple grep

 

6.9 Stripping the Man Pages

 

6.12 Limit the Size of the Core

6.3 Which tmp Is a Good Temp?

 

6.7 Executing Commands Recursively with find

 

6.10 Clean Up DOS Files

 

6.13 uuencode and uudecode

6.4 Dealing with Symbolic Links

 

 

 

 

 

 

 

6.6 Multiple grep

6.6.1 Description

6.6.1 Description

There are various ways that searching for multiple patterns can be executed on a file or piped from a process.

Example One: Using grep Again and Again

Flavors: AT&T, BSD

Syntax:

grep [pattern] file | grep [pattern]
command | grep [pattern] file | grep [pattern]

A series of grep commands can be joined together to create multiple searches through a file. The multiple searches are treated as a Boolean AND function when properly executed. If you have to search for a set of users in a specific group that has home directories crossing and automounting to a remote system, you can use a multiple grep one-line command:

rocket 41% cat /etc/passwd
garyd:.u0Iwe7OAFzcE:1111:20:Gary Daniels:/hosts/planet/usr/people/garyd:/bin/csh
swats:mEbqrshw206eU:1112:10:Scott Watsen:/hosts/planet/usr/people/swats:/bin/csh
jeffm:dunFf/jigfkH.:1113:20:Jeff Mirch:/usr/people/jeffm:/bin/csh
kricer:q/oSdx1Qkeqm.:1114:10:Kevin Ricer:/usr/people/kricer:/bin/csh
rocket 42% grep ":10:" /etc/passwd | grep "/hosts/planet" swats:mEbqrshw206eU:1112:10:Scott Watsen:/hosts/planet/usr/people/swats:/bin/csh

In this command, the only records displayed in the password file are those users who are in group 10 and whose home directories go through an automount point to the remote system planet.

You can also search with multiple grep commands on the output of a command. If you want to display all the NFS-mounted filesystems that point to a specific system, you can grep out the NFS hard-mounted filesystems and the name of a specific system from the df command.

rocket 43% df -k
Filesystem         Type  kbytes     use    avail  %use Mounted on
/dev/root           xfs  2051936  1752436  299500  86  /
/dev/dsk/dks023s7   xfs  1627996  1571004   56992  97  /saturn2
saturn:/usr         nfs  8360424  5198859  3170565 62 /tmp_mnt/hosts/saturn/usr
saturn:/patches     nfs  8360424  5099960  3260464  62  /patches
pluto:/var          nfs   966304   928110    38194  97  /tmp_mnt/hosts/pluto/var
pluto:/disk2        nfs  2042528  1992506    50022  98  /tmp_mnt/hosts/pluto/disk2
rocket 44% df -k | grep saturn | grep nfs | grep -v hosts saturn:/patches nfs 8360424 5099960 3260464 62 /patches

In this command, running df displays the current mounted filesystems on the system. When the three grep commands are put together, you request all lines that have the words saturn and nfs, and not hosts, be displayed. This yields all the NFS hard-mounted filesystems.

Example Two: Searching with egrep

Flavors: AT&T, BSD

Syntax:

egrep '([pattern1]|[pattern2]|[...])' file
command | egrep '[pattern1]|[pattern2]|[...]'

If you need to search for multiple patterns from the output of an executed command or from a file, using egrep is the best way to accomplish this task. Unlike using multiple grep commands, multiple patterns can be passed into egrep and are treated as a Boolean OR function.

If a search is made for all those in group 10 who have home directories that cross an automount that is NFS-mounted to planet, this typically must be done in two commands.

rocket 45% cat /etc/passwd
garyd:.u0Iwe7OAFzcE:1111:20:Gary Daniels:/hosts/planet/usr/people/garyd:/bin/csh
swats:mEbqrshw206eU:1112:10:Scott Watsen:/hosts/planet/usr/people/swats:/bin/csh
jeffm:dunFf/jigfkH.:1113:20:Jeff Mirch:/usr/people/jeffm:/bin/csh
kricer:q/oSdx1Qkeqm.:1114:10:Kevin Ricer:/usr/people/kricer:/bin/csh
rocket 46% cat /etc/passwd | grep ":10:"; cat /etc/passwd | grep "/hosts/planet" garyd:.u0Iwe7OAFzcE:1111:20:Gary Daniels:/hosts/planet/usr/people/garyd:/bin/csh swats:mEb kricer:q/oSdx1Qkeqm.:1113:10:Kevin Ricer:/usr/people/kricer:/bin/csh kricer:q/oSdx1Qkeqm.:1114:10:Kevin Ricer:/usr/people/kricer:/bin/csh

Perform a cat, grep for the group number 10, then perform another cat of the password file, and grep from the remote home directory. You can combine these two commands into one with the use of egrep.

rocket 47% egrep '(:10:|/hosts/planet)' /etc/passwd
garyd:.u0Iwe7OAFzcE:1111:20:Gary Daniels:/hosts/planet/usr/people/garyd:/bin/csh
swats:mEbqrshw206eU:1112:10:Scott Watsen:/hosts/planet/usr/people/swats:/bin/csh
kricer:q/oSdx1Qkeqm.:1114:10:Kevin Ricer:/usr/people/kricer:/bin/csh

The egrep searches for all occurrences of the patterns within the parentheses, with a vertical bar ( |) separating each pattern listed. The output then reflects all those in the password file that are part of group 10 or have /hosts/planet in the record line. All others are discarded.

A command can be executed and the output piped to egrep that can search for several patterns. If you want to display all the NFS mounted filesystems that point to a specific system, you can grep out the NFS hard-mounted filesystems and the name of a specific system from the df command.

rocket 48% df -k
Filesystem         Type  kbytes     use     avail %use Mounted on
/dev/root           xfs  2051936  1752436   299500  86 /
/dev/dsk/dks023s7   xfs  1627996  1571004    56992  97 /saturn2
saturn:/usr         nfs  8360424  5198859  3170565  62 /tmp_mnt/hosts/saturn/usr
saturn:/patches     nfs  8360424  5099960  3260464  62 /patches
pluto:/var          nfs   966304   928110    38194  97 /tmp_mnt/hosts/pluto/var
pluto:/disk2        nfs  2042528  1992506    50022  98 /tmp_mnt/hosts/pluto/disk2
rocket 49% df -k | egrep 'xfs|saturn' /dev/root xfs 2051936 1752436 299500 86 / /dev/dsk/dks023s7 xfs 1627996 1571004 56992 97 /saturn2 saturn:/usr nfs 8360424 5099960 3260464 62 /tmp_mnt/hosts/saturn/usr saturn:/disk2 nfs 71077760 33350188 37727572 47 /disk2

When the df command is executed, it displays the current mounted filesystems on the system. When df pipes the two patterns to egrep, the result displays every line that has xfs and saturn in it.

Reason

The use of a single egrep command provides an administrator with a tool for extracting the all the necessary data that otherwise would have to be done in multiple commands.

Other Resources

Man pages:

cat, df, egrep, grep

UNIX Hints & Hacks

ContentsIndex

Chapter 6: File Management

 

Previous ChapterNext Chapter

Sections in this Chapter:

   

6.1 Copy Files with Permissions and Time Stamps

 

6.5 Finding Files with grep

 

6.8 Moving and Renaming Groups of Files

 

6.11 Splitting Files

6.2 Copy Files Remotely

 

6.6 Multiple grep

 

6.9 Stripping the Man Pages

 

6.12 Limit the Size of the Core

6.3 Which tmp Is a Good Temp?

 

6.7 Executing Commands Recursively with find

 

6.10 Clean Up DOS Files

 

6.13 uuencode and uudecode

6.4 Dealing with Symbolic Links

 

 

 

 

 

 

 

© Copyright Macmillan USA. All rights reserved.