On 28-Feb-99 PAUL TRACESKI wrote:
> I've been scratching my head over this one. I have a list of data that I
> want to selectively edit prior to sorting. The data is in two forms:
>
> ABDC 111 */4 letters, 3 spaces, 3 digits
> ABCD 22 */4 letters, 3 spaces, 2 digits
>
> I wish to add a leading 0 to the digits in the lines containing only two
> digits, leaving the current 3 digit lines as is:
>
> ABDC 111 */4 letters, 3 spaces, 3 digits
> ABCD 022 */4 letters, 3 spaces, 2 digits
>
> I can add a leading zero to the digits in all lines with:
>
> sed 's/ / 0/g' file1 > file2
>
> Is there a way to set the address option for sed to just perform
> the editing on those lines matching a pattern:
>
> / [0-9][0-9]/ or the negation of /[0-9][0-9][0-9]/
Yes. Use the "b" command to jump to a label.
Example:
sed -e '\x.*[0-9]\{3\}xb' -e 's/ / 0/' file1 > file2
Here we have two commands, so we need to use the "-e" option to specify them.
'\x.*[0-9]\{3\}xb'
The "\x" indicates that the character "x" is to be used as a delimiter to
enclose the regular expression. Any character can be used. The second "x" at
the end close the regular expression.
The ".*" match any number of any character, and the "[0-9]\{3\}" matches
exactly 3 numbers. The backslash are used to protect the braces.
The "b" command instruct sed to jump to the label following "b" when the
regular expression preceding it matches. In this example, the label is missing,
indicating that sed should jump to the end, hence skipping the substitute
command.
For details, refer to the sed man page.
Cort
[EMAIL PROTECTED]