Find and Replace characters in a column range using Regex
-
I need to check columns 50 thru 55 for a - and if found replace with a . My file has 1M lines.
I have figured out how to start it for column 50 but how do i repeat this for the next 4 columns.
Find what: ^.{49}\K-
Replace with: . -
@Bob-Johnson said in Find and Replace characters in a column range using Regex:
how do i repeat this for the next 4 columns
The simplest way would be to use your original regex, but change the 49 to 50, then 51 etc. So in effect you run the regex 5 times in total, increasing the number by 1 each time.
It is possible to create another regex to cater for 1,2,3,4 or 5 - in a row that only needs running once, but I don’t think it’s worth the effort, especially if what you are editing is not a regular occurrence.
Terry
-
Hello, @bob-johnson, @terry-r and All,
OK, Bob. Luckily your S/R does not change the columns order, as you just want to replace
1char with a single char, too. So, given the sample text below :Column 4455555555 8901234567 0 ABCDE VWXYZ - 12345 # - at column 48 0 ABCDE VWXYZ - 12345 # - at column 49 0 ABCDE VWXYZ -BCDEFG 12345 # - at column 50 0 ABCDE VWXYZ A-CDEFG 12345 # - at column 51 0 ABCDE VWXYZ AB-DEFG 12345 # - at column 52 0 ABCDE VWXYZ ABC-EFG 12345 # - at column 53 0 ABCDE VWXYZ ABCD-FG 12345 # - at column 54 0 ABCDE VWXYZ ABCDE-G 12345 # - at column 55 0 ABCDE VWXYZ - 12345 # - at column 56 0 ABCDE VWXYZ - 12345 # - at column 57The following regex S/R :
-
SEARCH
(?-s)^.{49}.{0,5}\K- -
REPLACE
. -
Tick the
Wrap aroundoption -
Click on the
Replace Allbutton -
And choose, of course, the
Regular expressionsearch mode
It should modify the text, as expected :
Column 4455555555 8901234567 0 ABCDE VWXYZ - 12345 # - at column 48 0 ABCDE VWXYZ - 12345 # - at column 49 0 ABCDE VWXYZ .BCDEFG 12345 # - at column 50 0 ABCDE VWXYZ A.CDEFG 12345 # - at column 51 0 ABCDE VWXYZ AB.DEFG 12345 # - at column 52 0 ABCDE VWXYZ ABC.EFG 12345 # - at column 53 0 ABCDE VWXYZ ABCD.FG 12345 # - at column 54 0 ABCDE VWXYZ ABCDE.G 12345 # - at column 55 0 ABCDE VWXYZ - 12345 # - at column 56 0 ABCDE VWXYZ - 12345 # - at column 57Notes :
-
The first part
(?-s)forces the regex engine to consider that dot (.) represents a single standard char only ( Not an EOL one ) -
The special syntax
\K, cancels any previous search, already matched, at current location of the regex engine and, in your case, just consider the last-character
Best Regards,
guy038
-
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login