home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 34.10 Referencing Portions of a Search String Chapter 34
The sed Stream Editor
Next: 34.12 Transformations on Text
 

34.11 Search & Replacement: One Match Among Many

One of the more unusual options of sed 's substitution command is the numeric flag that allows you to point to one particular match when there are many possible matches on a particular line. It is used where a pattern repeats itself on a line and the replacement must be made for only one of those occurrences by position. For instance, a line, perhaps containing tbl input, might contain multiple tabs. Let's say that there are three tabs per line, and you'd like to replace the second tab with > . The following substitute command would do it:

s/
[TAB]
/>/2

[TAB] represents an actual tab character, which is otherwise invisible on the screen. If the input is a one-line file such as the following:

Column1
[TAB]
Column2
[TAB]
Column3
[TAB]
Column4

the output produced by running the script on this file will be:

Column1
[TAB]
Column2>Column3
[TAB]
Column4

Note that without the numeric flag, the substitute command would replace only the first tab. (Therefore, 1 can be considered the default numeric flag.) The range of the numeric value is from 1 to 512.

- DD from O'Reilly & Associates' sed & awk , Chapter 5


Previous: 34.10 Referencing Portions of a Search String UNIX Power Tools Next: 34.12 Transformations on Text
34.10 Referencing Portions of a Search String Book Index 34.12 Transformations on Text

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System










??????????????@Mail.ru