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
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, - from O'Reilly & Associates' sed & awk , Chapter 5 |
|