2

A file with two columns looks like the following:

Column A Column B
31.03.2024 01:00 1.002
31.03.2024 03:00 2.003
31.03.2024 05:00 3.007
31.03.2024 05:00 4.985
31.03.2024 06:00 2.987
-------- --------

There are two lines with the same DateTime entry 31.03.2024 05:00. I need to change the first one to 31.03.2024 04:00and leave the values in Column B unchanged. I can find it, but how to replace only the first one?

My trial to locate the correct area: ^[0-9]{1,2}.[0-9]{1,2}.[0-9]{4}\s05:00$\n^[0-9]{1,2}.[0-9]{1,2}.[0-9]{4}\s05:00$

desertnaut
60.8k32 gold badges155 silver badges183 bronze badges
asked Feb 10, 2025 at 18:32
6
  • what's the constraint on using regex to solve this? Commented Feb 10, 2025 at 18:39
  • No constraint at all. I am open to any other suggestions! Commented Feb 10, 2025 at 18:59
  • Load it into an array, dataframe, database table, etc. and use the language's features for manipulating it. Commented Feb 10, 2025 at 19:46
  • Answering this probably needs knowledge of which regex version is being used. Different implementations have different ways of doing these more challenging operations. Commented Feb 10, 2025 at 19:54
  • As was already asked, the regular expression engine version is needed to give an exact solution. However the process I'd use would be to look for the string you want and at the tail end of the regular expression add in a lookahead for the exact same string. As the lookahead would span multiple lines (you show the very next line is the repeated value, would that always be true?) the code for that depends on your regex version. The extra code would look something like (?=.+?1円) where 1円 is the first group so you would want to include your code in () to make it the first group. Commented Feb 11, 2025 at 2:36

2 Answers 2

3

These regex can be used that will match your line and replace with the alternate time. Since the replacement requires a capture group, they are language specific syntax to avoid capture group number conflicting next to a number in the replacement.

PCRE:

(?m)^([0-9]{1,2}\.[0-9]{1,2}\.[0-9]{4}\s*)(05:00)(?=.*\s+^1円2円)

replace ${1}04:00
https://regex101.com/r/Q5GG8B/1

Java:

(?m)^(?<g1>[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{4}\s*)(05:00)(?=.*\s+^1円2円)

replace ${g1}04:00
https://regex101.com/r/YF1D7G/1

Python:

(?m)^([0-9]{1,2}\.[0-9]{1,2}\.[0-9]{4}\s*)(05:00)(?=.*\s+^1円2円)

replace \g<1>04:00
https://regex101.com/r/e9ZH1t/1

Dot-net:

(?m)^([0-9]{1,2}\.[0-9]{1,2}\.[0-9]{4}\s*)(05:00)(?=.*\s+^1円2円)

replace ${1}04:00
https://regex101.com/r/wjmKn5/1

answered Feb 11, 2025 at 20:19
Sign up to request clarification or add additional context in comments.

Comments

0

Not sure why you guys would want to write regexs so overcomplicated like that. Because you only want to change 31.03.2024 05:00 to 31.03.2024 04:00, why not make the regex exactly that?

s/31.03.2024 05:00/31.03.2024 04:00/

That will change the first occurrence. If you wanted to change both, make it a global search by adding the /g option to the end. I put the data in a file like this...

$ cat change.column.value.txt 
Column A Column B
31.03.2024 01:00 1.002
31.03.2024 03:00 2.003
31.03.2024 05:00 3.007
31.03.2024 05:00 4.985
31.03.2024 06:00 2.987
-------- --------

I wrote my solution in Perl, the code looks like this...

#!/usr/bin/perl -w
undef $/; #read entire file at once, only change first instance
while(<>){
 s/31.03.2024 05:00/31.03.2024 04:00/;
 print;
}

Output looks like this...

$ perl change.column.value.pl change.column.value.txt
Column A Column B
31.03.2024 01:00 1.002
31.03.2024 03:00 2.003
31.03.2024 04:00 3.007
31.03.2024 05:00 4.985
31.03.2024 06:00 2.987
-------- --------

If you wanted to create a new file with the new data you could run something like this...

$ perl change.column.value.pl change.column.value.txt > updated.column.value.txt

If you wanted to change the existing file, you could use the -i option like this...

$ perl -i change.column.value.pl change.column.value.txt

Golfed at 36 characters

$ perl -0777 -pe 's/31.03.2024 05:00/31.03.2024 04:00/' change.column.value.txt
Column A Column B
31.03.2024 01:00 1.002
31.03.2024 03:00 2.003
31.03.2024 04:00 3.007
31.03.2024 05:00 4.985
31.03.2024 06:00 2.987
-------- --------
gre_gor
6,65812 gold badges105 silver badges106 bronze badges
answered Mar 7, 2025 at 8:32

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.