DistributedNetworks DistributedNetworks

Shell Variables   «Prev 

Using UNIX Commands for String Manipulation

Three UNIX commands for string manipulation

Several UNIX commands are particularly useful when using a shell script for complex string manipulations. This page mentions only three of them. To analyze a string stored in a shell variable, you generally must use embedded command execution, which is covered in the next course.
  1. The wc command displays the number of characters in a stream of input (the variable value). This can be used to determine the length of a string.
  2. The cut command can extract either a whitespace-separated field or a set of characters from a string. This can be used to extract a sub-string or examine the fields of the wc command.
  3. The sed command can perform complex manipulations on a line of text, searching for patterns and deleting or adding text based on various rules.

Sed Stream Editor

sed - manual page for sed version 4.0.3 

sed [OPTION]... {script-only-if-no-other-script} [input-file]...  

Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text in a pipeline which particularly distinguishes it from other types of editors.