11.6 Text Processing

   

You have used the grep command as a filter to extract or delete lines containing a particular text pattern. Here you will learn two more commands that are useful for text processing. The sed command is a stream editor that takes text from stdin and sends it to stdout after editing it. The cut command is used to extract a desired part of text from a line. It also takes its input from stdin and sends its output to stdout.

Using sed

The stream editor is a useful tool to edit large amounts of text at one time. For example, you may need to search for a word in a large file and replace it with another word. Let's try to replace the word " echo " with " ECHO " in script-27 . The sed command will do the job as follows .

 $  sed s/echo/ECHO/g script-27  #!/usr/bin/sh NUM=100 while true do    ECHO "Enter a divisor for integer 100 : \c"    read DIV    if [ $DIV -eq 0 ]    then       ECHO "Divide by zero is not permitted"       exit 1    fi    (( QUO = NUM / DIV ))    (( REM = NUM % DIV ))    ECHO "The quotient is  : $QUO"    ECHO "The remainder is : $REM" done 

If you want to do an operation on all files, you can write a shell program to accomplish the job. Program script-28 shown here replaces " echo " with " ECHO " in all files of the current directory.

 #!/usr/bin/sh for FILE in * do    cat $FILE sed s/echo/ECHO/g >tempfile    cp tempfile $FILE done rm tempfile 

As you can see, this is a very useful tool to make changes to a large number of files that could take a long time otherwise . Consider you are writing a book and want to change "figure" to "Fig" in all chapters. If you don't know how to do it in an efficient way, you may start editing all files manually, spending hours on a job that need take only a few minutes.

There are many ways to use sed that make it a very useful tool. For additional information, consult the sed manual pages.

Using cut

The cut command is used to extract a particular part of data from a line of text. If the data are in the form of fields, you can extract particular fields. For example, if you want to list all user names on your system, you can use the cut command on the /etc/passwd file as follows:

 cut -f 1 -d : /etc/passwd 

or

 cat /etc/passwd  cut -f 1 -d : 

Here the -f 1 option tells the command that you want to extract field number 1. The -d : option shows that the fields in the data are separated by a delimiter colon ":". Since user names are in the start of each line in /etc/passwd and they are followed by a colon, the command extracts all user names from the file.

You may also use the cut command to extract a particular number of characters from a file. To extract the first eight characters from every line in the /etc/passwd file, you may use the following command.

 cat /etc/passwd  cut -c 1-8 

Here you specified a range of characters using the -c 1-8 option. See the manual pages for more information on the cut command.

Let us use the cut command in a shell program script-29 . This script is used to send an email message to all users on a system. The message contents are stored in a file mailfile . You use the mailx command to send the message.

 #!/usr/bin/sh for USER in $(cut -f 1 -d : /etc/passwd) do    mailx -s "Test mail" $USER <mailfile done 

You have used the cut command to create a list of user names and then send a mail message to each name in the list.

The sleep Command

The sleep command is used to suspend execution for a certain amount of time. You provide the number of seconds as the argument to the sleep command. The following code segment lists all files in the current directory with a pause of five seconds between every file.

 for FILE in * do    ll $FILE    sleep 5 done 

   
Top


HP Certified
HP Certified: HP-UX System Administration
ISBN: 0130183741
EAN: 2147483647
Year: 2000
Pages: 390
Authors: Rafeeq Rehman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net