Converting decimal value of some numbers using Grep

63 views
Skip to first unread message

Howard

unread,
Mar 16, 2022, 1:02:58 PM3/16/22
to BBEdit Talk
I have a column of numbers; some have a decimal value. Some numbers end with `.1` and some end with `.2`. If a number's decimal part contains `.1` I want to change its ending to `.33`, but keep its whole number the same, so 1.1 would become 1.33. Also,  if a  number's decimal part contains `.2` I want to change its ending to `.67`, but keep its whole number the same, so 1.2 would become 1.67.

As the column can have hundreds of rows, using Grep is there an easy way to convert the decimal portion of a number that ends with either .1 or .2 while retaining the whole number part?

Howard

Sample      Desired output
0.2                    0.67
1.1                    1.33
1.1                    1.33
1.2                    1.67
1.1                    1.33
6                        6
3.2                     3.67
8                        8
6.2                     6.67

Patrick Woolsey

unread,
Mar 16, 2022, 1:41:06 PM3/16/22
to bbe...@googlegroups.com
Though you can do this, you'll need to perform two search & replace passes: one for each value pair.

Find: ^(\d+?)\.1

Replace: \1.33

and

Find: ^(\d+?)\.2

Replace: \1.67


Regards,

Patrick Woolsey
==
Bare Bones Software, Inc. <https://www.barebones.com/>

Howard

unread,
Mar 16, 2022, 3:10:59 PM3/16/22
to BBEdit Talk
Patrick,

In Find: ^(\d+?)\.1 what is the purpose of the question mark? I know what \d+ does.

Howard

Patrick Woolsey

unread,
Mar 16, 2022, 5:24:21 PM3/16/22
to bbe...@googlegroups.com
On 3/16/22 at 3:10 PM, leadwi...@gmail.com (Howard) wrote:

>In *Find: ^(\d+?)\.1 *what is the purpose of the question mark?
>I know what *\d+* does.
>

It makes matching "non-greedy", so the pattern won't try to
extend beyond the first possible match.


[For details, please see "Non-Greedy Quantifiers" in Chapter 8
(page 201) of the ubiquitous PDF manual. :-) ]

Howard

unread,
Mar 16, 2022, 7:21:53 PM3/16/22
to BBEdit Talk
Patrick,

Thanks for your help,

Howard

jj

unread,
Mar 17, 2022, 3:08:31 AM3/17/22
to BBEdit Talk
Howard,

If it applies to you case you could use a literal find/replace.
Uncheck the Grep checkbox in the Find/Replace window.

    Find: .1
    Replace: .33
   
    Find: .2
    Replace: .67
   
Just my .2 cents.

Best Regards,

Jean

Neil Faiman

unread,
Mar 17, 2022, 7:59:00 AM3/17/22
to BBEdit Talk Mailing List
On Mar 16, 2022, at 5:24 PM, Patrick Woolsey <pwoo...@barebones.com> wrote:


In *Find: ^(\d+?)\.1 *what is the purpose of the question mark? I know what *\d+* does.


It makes matching "non-greedy", so the pattern won't try to extend beyond the first possible match.

In this particular case, the ? doesn’t have actually have any effect — the longest string of digits that is immediately followed by a dot is exactly the same as the shortest 
String of digits that is  immediately followed by a dot, since a dot is not a digit. But the non-greedy modifier can be essential when a repeated wild-card pattern would otherwise match right over what is supposed to be the terminator. 

Regards,

Neil Faiman

Patrick Woolsey

unread,
Mar 17, 2022, 9:56:38 AM3/17/22
to bbe...@googlegroups.com
On 3/17/22 at 7:58 AM, neil....@faiman.org (Neil Faiman) wrote:

>>On Mar 16, 2022, at 5:24 PM, Patrick Woolsey <pwoo...@barebones.com> wrote:
>>
>>> In *Find: ^(\d+?)\.1 *what is the purpose of the question mark? [...]
>>
>>It makes matching "non-greedy", so the pattern won't try to extend beyond the first possible match.
>
>In this particular case, the ? doesn’t have actually have any
>effect — the longest string of digits that is immediately
>followed by a dot is exactly the same as the shortest String of
>digits that is immediately followed by a dot, since a dot is
>not a digit. But the non-greedy modifier can be essential when
>a repeated wild-card pattern would otherwise match right over
>what is supposed to be the terminator.

That's correct, provided the shape of the actual data exactly
parallels that of the example but I've learned to take
precautions whenever possible. ;-)
Reply all
Reply to author
Forward
0 new messages