Is it possible to replace character in specific place in each line?
I.e. in line "2012-09-19 18:00:00 CDT" I have blanks on position 11 and position 20.
I need to replace them with _.
I cannot use ^b because I need to keep spaces in other part of the line.
For your example the Perl regular expression search string is
"^(.{10}) (.{8}) "
(without the double quotes just used here to show you that the search string ends with a space character)
and the replace string is
\1_\2_
^ means start every search at beginning of a line.
(...) capture a part of the found string for being referenced and re-used during replace. In this search string are two such round brackets pair capturing the date and the time strings referenced with \1 and \2 in replace string.
. matches any character except carriage return and line-feed.
{10} means preceding expression (any character) should be found and matched exactly 10 times.
So the entire expression means:
Find a string beginning at start of a line
with 10 characters (date)
and a single space
and 8 more characters (time)
and one more single space
and replace this string by found date string plus an underscore plus found time string plus an underscore.
Alternatively you could open Advanced options of Replace dialog, check option Search in Column, enter 0 and 20 as column range and run a non regular expression Replace All searching for a single space and replacing it with a single underscore in that column range.
Thank you for the reply.
The solution works, with one small problem - it replaced spaces " " with an underscore character and a space "_ ".
Search line:
^(.{8})(.{11})(.{22})(.{10})(.{10})
Replace line:
\1_\2_\3_\4_\5_
Well, your replace did not work as you don't have used what I have written. My search string was:
^(.{10})space(.{8})space
So in my search string the 2 single spaces to replace by 2 single underscores where not included in the round brackets. The expression .{x} matches only the number of non space characters.
You have obviously included the space character also in the tagged expression within the round brackets. Therefore your replace inserts underscores instead of replacing the spaces.
Perhaps it is possible to use for your file the search string "^([^ ]+?) ([^ ]+?) ([^ ]+?) ([^ ]+?) ([^ ]+?) " (without the double quotes) and as replace string \1_\2_\3_\4_\5_.
Please note: The search expression contains 5 single spaces within the 5 square brackets and 5 single spaces after every ).
[^ ]+? finds a string with 1 or more characters NOT containing a space character.
Or you simply use the replace in column range feature to replace the spaces as I suggested also in my previous post.
This is my last try to explain you how to replace spaces in some columns. I have posted twice correct search strings always with a single space between the round brackets and at end of the search string and you consistently ignore my search strings and use search strings not containing any space character. I explained twice very detailed the expressions, but you do not follow my explanations and create search strings where the spaces which should be replaced by underscores are matched also by the expressions inside the round brackets. That can't work and that's not what I have written. Please follow my suggestions.
only by using search string (without double quotes):
"^(.{11})(.{6})(.{11})(.{10}) " <- my string is longer because of the four spaces
"^(.{11})(.{8})(.{11})(.{11})" <- your string is shorter because of missing the spaces and wrong character multiplier numbers
My search string contains four spaces - three single spaces between the round brackets and one at end. Your search string does not contain any space character. My search string matches with expression in second round bracket just six characters. Your search string matches eight characters and matches therefore also the space character before and after string Zone R which should be replaced. And same mistake is made also for fourth expression.
The replace string is \1_\2_\3_\4_
As last chance for you to understand that the space characters to replace must be outside the round brackets, I post here a screenshot with additional red color marking the four spaces present in search string which are replaced by four underscores.
Replace dialog for replacing space characters at column 12, 19, 31 and 42.
min2max, your more generic regular expression is not good. In general when running a Replace All it should be one goal to avoid wrong replaces as much as possible. Every string which does not match the requirements which should be as restrictive as possible should not be modified by a Replace All command.
If Bigll would have used your generic regular expression, he would have always success on running the Replace All, but the result would be destroyed data because of the wrong multiplier numbers in the braces. If the generic regular expression Replace All would have been executed on a large or even huge file opened without usage of a temporary file, the modifications by the wrong defined Replace All would have been unrecoverable because Undo would have been not available and other characters than the spaces would have been replaced by underscores.
It is advisable to make a search regular expression for a replace as restrictive as possible. If the requirement is to replace spaces in certain columns by underscores, it is not advisable if the search string matches also other characters than spaces for replacement.
However, for replacing characters at a specific column in a file there is the alternate method of using the replace in columns feature of UltraEdit as explained in my first post which is more easy to use for UltraEdit users not familiar with Perl regular expressions. And if such a replace must be done in all lines of a file or just all lines of a selected block, it is also possible to use the column editing mode and the commands Column - Delete Columns and Column - Insert/Fill Columns or simply selecting and typing in column editing mode which I use very often instead of regular expression replaces or replace in columns.
Your advice of 'making a search regular expression for a replace as restrictive as possible' to avoid unintentional damage to a file is definitely right.
What I meant is that my solution matched the original post's title, "Replace character in specific place in each line", literally, more tightly.
By the way, before your explanation, I was actually even not aware of the solution you mentioned using the 'Column' menu in case we don't care whether the characters at the specified column are a space or anything else.
min2max wrote:How can I replace a character in specific place, counting from the end, in each line?
If for example the fifth character before end of a line should be replaced by a tab character if this character is a semicolon, it can be done with one of the 3 expression below:
With the Perl regular expression engine:
Find What: ;(.{4})$
Replace With: \t\1
With the legacy Unix regular expression engine:
Find What: ;(....)$
Replace With: \t\1
This search string can be used also with the Perl regular expression engine.
With the legacy UltraEdit regular expression engine: