Using sed Utility215
7.2Using sed Utility
The
sed
utility is a stream editor that can be used for different file editing purposes when used
as a filter. The most common task for software development purposes is the use of
sed
to search
and replace text in source code files. Let us take the example of the following C source code file
hello.c
.
#include
main ()
{
char string[25];
printf ("Enter a line of characters : ");
scanf ("%s", string);
printf ("The entered string is : %s\n ", string);
}
In order to replace every occurrence of word
string
with the word
STRING
, you can use
sed
. The
sed
filter command and its result on this file are shown below.
[root@boota]# cat hello.c | sed s/string/STRING/
#include
main ()
{
char STRING[25];
printf ("Enter a line of characters : ");
scanf ("%s", STRING);
printf ("The entered STRING is : %s\n ", string);
}
[root@boota indent]#
The
sed
command understands UNIX regular expressions. Regular expressions can be
used for a higher level of stream editing. You can also use
sed
in shell scripts as well as make-
files to carry out tasks in the entire source code directory tree. You can also use –
f
options fol-
lowed by a filename. The filename contains
sed
commands. Please refer to the
sed
man pages
for a complete set of options.
7.3Using diff Utility
The
diff
utility is another useful tool that developers may need. It is used to find out the differ-
ences between two files. If you are using CVS, differences between different versions of a file in
the CVS repository can be found using the
cvs
(
cvs diff
) command as well. However, if
you want to find out the difference between two files that are not in the CVS repository, the
diff
utility may be quite handy. One common use may be to find out the difference between
the working copy and the backup copy of a source code file. This will enable you to find out
Next Page >>
<< Previous Page
Back to the Table of Contents