Replace entire line based on first characters
-
Hello, how do I replace every line that starts with “X”: 50. with “X”: 50.0,
There are random numbers after every “X”: 50. and I want them to be set to 0Here is the document I’m working on (I’ve removed some lines to fit in the post). I’ve tried doing it one by one, but I’m hoping someone could help me find a shortcut.
{
“Events”: [
{
“Delta”: 0,
“EventType”: “MouseDown”,
“Timestamp”: 603,
“X”: 50.0,
“Y”: 79.61000061035056
},
{
“Delta”: 0,
“EventType”: “MouseMove”,
“Timestamp”: 683,
“X”: 50.0,
“Y”: 79.50000213623050
},
{
“Delta”: 0,
“EventType”: “MouseMove”,
“Timestamp”: 687,
“X”: 50.0,
“Y”: 78.93000030507578
},
{
“Delta”: 0,
“EventType”: “MouseMove”,
“Timestamp”: 699,
“X”: 50.0,
“Y”: 78.26000213623050
},
{
“Delta”: 0,
“EventType”: “MouseMove”,
“Timestamp”: 703,
“X”: 50.0,
“Y”: 76.9000015258789
},
{
“Delta”: 0,
“EventType”: “MouseMove”,
“Timestamp”: 716,
“X”: 50.0,
“Y”: 75.26000213623050
},
{
“Delta”: 0,
“EventType”: “MouseMove”,
“Timestamp”: 719,
“X”: 50.0,
“Y”: 73.0
},
{
“Delta”: 0,
“EventType”: “MouseMove”,
“Timestamp”: 730,
“X”: 50.0,
“Y”: 71.0
},
{
“Delta”: 0,
“EventType”: “MouseMove”,
“Timestamp”: 891,
“X”: 50.880001068115234,
“Y”: 41.540000915527344
},
{
“Delta”: 0,
“EventType”: “MouseMove”,
“Timestamp”: 895,
“X”: 50.77000045776367,
“Y”: 40.66999816894531
},
{
“Delta”: 0,
“EventType”: “MouseMove”,
“Timestamp”: 906,
“X”: 50.720001220703125,
“Y”: 39.5099983215332
},
{
“Delta”: 0,
“EventType”: “MouseMove”,
“Timestamp”: 911,
“X”: 50.61000061035056,
“Y”: 38.55099923706055
},
{
“Delta”: 0,
“EventType”: “MouseMove”,
“Timestamp”: 923,
“X”: 50.61000061035056,
“Y”: 38.060001373291016
},
{
“Delta”: 0,
“EventType”: “MouseMove”,
“Timestamp”: 926,
“X”: 50.61000061035056,
“Y”: 37.38999938965044
},
{
“Delta”: 0,
“EventType”: “MouseMove”,
“Timestamp”: 939,
“X”: 50.5,
“Y”: 36.61000061035056
},
{
“Delta”: 0,
“EventType”: “MouseMove”,
“Timestamp”: 943,
“X”: 50.45000076293945,
“Y”: 35.84000015258789
},
{
“Delta”: 0,
“EventType”: “MouseMove”,
“Timestamp”: 955,
“X”: 50.34000015258789,
“Y”: 35.15999985041211
},
{
“Delta”: 0,
“EventType”: “MouseMove”,
“Timestamp”: 959,
“X”: 50.34000015258789,
“Y”: 34.5000016784668
},
{
“Delta”: 0,
“EventType”: “MouseMove”,
“Timestamp”: 971,
“X”: 50.34000015258789,
“Y”: 33.709999084502656
},
{
“Delta”: 0,
“EventType”: “MouseMove”,
“Timestamp”: 975,
“X”: 50.34000015258789,
“Y”: 33.0
},
{
“Delta”: 0,
“EventType”: “MouseMove”,
“Timestamp”: 986,
“X”: 50.34000015258789,
“Y”: 32.16999816894531
},
{
“Delta”: 0,
“EventType”: “MouseMove”,
“Timestamp”: 991,
“X”: 50.34000015258789,
“Y”: 31.399999618530273
},
{
“Delta”: 0,
“EventType”: “MouseMove”,
“Timestamp”: 1003,
“X”: 50.34000015258789,
“Y”: 30.719999313354502
},
{
“Delta”: 0,
“EventType”: “MouseMove”,
“Timestamp”: 1007,
“X”: 50.34000015258789,
“Y”: 30.0
},
{
“Delta”: 0,
“EventType”: “MouseMove”,
“Timestamp”: 1019,
“X”: 50.34000015258789,
“Y”: 29.3700008392334
},
{
“Delta”: 0,
“EventType”: “MouseMove”,
“Timestamp”: 1023,
“X”: 50.34000015258789,
“Y”: 28.690000534057617
},
{
“Delta”: 0,
“EventType”: “MouseMove”,
“Timestamp”: 1886,
“X”: 50.95000076293945,
“Y”: 6.659999850412109
},
{
“Delta”: 0,
“EventType”: “MouseUp”,
“Timestamp”: 1950,
“X”: 50.95000076293945,
“Y”: 6.659999850412109
}
]
} -
In the future, please wrap text samples inside of ``` so that they show up like this:
{"this is the proper way to display text": true}
In any case, you are working with JSON. Other people might suggest using regular expressions. This is a bad idea.
I recommend installing the JsonTools plugin and running the following query:
@.Events[:].X = 50.0
Once you run that query, every
X
value should be converted to50.0
, and the document will also be pretty-printed.This query converts the
X
field of each element in theEvents
field of the root JSON to the number50.0
. To learn more about the syntax of this query, I recommend reading this documentation on the RemesPath query language.That query assumes that the structure of the file is more or less as you showed it. If that query raises an error, I will need to know more about the structure of your JSON.
-
@SilverMew22 said in Replace entire line based on first characters:
Hello, how do I replace every line that starts with “X”: 50. with “X”: 50.0,
There are random numbers after every “X”: 50. and I want them to be set to 0Do it as a regular expression search/replace
Search:(?-i)^(\s*"X":\s*50\.)\d*
Replace:\1\x30
Parsing that from left to right:
(?-i)
- Make this a case-sensitive search/replace so that “X” does not match “x”.^
- Start at the beginning of a line.(\s*"X":\s*50\.)
- This is a capture group as it’s inside the parentheses. It contains:\s*
- Match zero or more spaces/tabs at the beginning of the line."X":\s*50\.
- Match the characters"X":
followed by\s*
zero or more spaces. followed by the characters50.
.\d*
- match zero or more digits
The replacement has:
\1
- this is the contents of the first capture group and will be the leading spaces followed by the `“X”: 50.’ that was matched and stored in the capture group.\x30
- This is an ASCII zero. I could have used a replacement of\10
but that sure looks like a ten and so I used\x30
instead of0
to reduce the chances of confusion.
This will change a
50.
without any following digits into50.0
. If you don’t desire that then this can be fixed.This also “replaces”
50.0
with50.0
which is no change but it made the search/replace expression simpler.I decided to ignore the comma at the end of the “X” lines. If it’s there then it remains there.
As @Mark-Olson noted, this should really be handled by something that is JSON aware. If by chance you have “X” elements inside of things other than the blocks you have in your example then those too will have a
50....
value changed to50.0
. -
@mkupper said in Replace entire line based on first characters:
\1\x30
Thank you so much, it worked. You saved me hours of time.
-
One change I would make to the regex of mkupper, if you’re going to use regular expressions:
replace(?-i)((?<!\\)"X"\s*:\s*50\.)\d*
with${1}0
Essentially the same, except:
- I get rid of the
^\s*
before the “X” key, and allow any amount of whitespace between the key and the colon, because JSON-parsing regexes should not make any assumptions about formatting. - The
(?<!\\)
before the open quote is just to guard against any weirdness with escaped quotes - I use the alternate
${1}
syntax to get the first capture group in the replacement regex, because I like that syntax better.
- I get rid of the
-
Hello, @silvermew22, @mkupper, @mark-olson and All,
I suppose that the following version should be better as it reduces the replacements to the syntaxes like
50.880001068115234
or50.5
only !SEARCH
(?-i)((?<!\\)"X"\s*:\s*50\.)(?!0,)\d+
Replace
${1}0
Now, in order to only get modifications, in the right
"Events"
array, we could use this version :SEARCH
(?-i)(?:"Events"|(?!\A)\G)(?s:(?!\x5D).)*?\K((?<!\\)"X"\s*:\s*50\.)(?!0,)\d+
REPLACE
${1}0
So :
- Paste your
JSON
file OR the example, below, in a new tab
{ "Tests": [ { "Delta": 0, "EventType": "MouseDown", "Timestamp": 603, "X": 50.0, "Y": 79.61000061035056 }, { "Delta": 0, "EventType": "MouseMove", "Timestamp": 891, "X": 50.880001068115234, "Y": 41.540000915527344 }, { "Delta": 0, "EventType": "MouseMove", "Timestamp": 939, "X": 50.5, "Y": 36.61000061035056 }, ] }, { "Events": [ { "Delta": 0, "EventType": "MouseDown", "Timestamp": 603, "X": 50.0, "Y": 79.61000061035056 }, { "Delta": 0, "EventType": "MouseMove", "Timestamp": 683, "X": 50.0, "Y": 79.50000213623050 }, { "Delta": 0, "EventType": "MouseMove", "Timestamp": 891, "X": 50.880001068115234, "Y": 41.540000915527344 }, { "Delta": 0, "EventType": "MouseMove", "Timestamp": 939, "X": 50.5, "Y": 36.61000061035056 }, { "Delta": 0, "EventType": "MouseUp", "Timestamp": 1950, "X": 50.95000076293945, "Y": 6.659999850412109 } ] }
-
Move the caret to the very beginning of the file ( Very IMPORTANT )
-
Open the Replace dialog (
Ctrl + H
) -
Untick all the box options
-
Click once on the
Replace All
button
Voila ! There were only
3
replacements in the unique"Events"
array… and none in the"Tests"
array !Best Regards,
guy038
- Paste your