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
1
char 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 57
The following regex S/R :
-
SEARCH
(?-s)^.{49}.{0,5}\K-
-
REPLACE
.
-
Tick the
Wrap around
option -
Click on the
Replace All
button -
And choose, of course, the
Regular expression
search 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 57
Notes :
-
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
-