Replace character in specific place in each line

Replace character in specific place in each line

4
NewbieNewbie
4

    Sep 19, 2012#1

    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.

    TIA, Bigll

    6,686585
    Grand MasterGrand Master
    6,686585

      Sep 20, 2012#2

      Yes, this is very easy with a tagged expression.

      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.

      4
      NewbieNewbie
      4

        Sep 20, 2012#3

        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_

        6,686585
        Grand MasterGrand Master
        6,686585

          Sep 21, 2012#4

          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.

          4
          NewbieNewbie
          4

            Sep 21, 2012#5

            Thank you for the help.

            Actually recommended by you search string works as expected :-)
            It's replace string adds spaces
            Unless I am missing something.

            I.e.

            For the text string:

            Time Date D Zone R Zone Source Address My Address

            I used:
            search string - "^(.{11})(.{8})(.{11})(.{11})"
            replace string - "\1_\2_\3_\4_"

            The result:
            Time Date D_ Zone R _Zone Source_ Address My_ Address.

            Please notice spaces after 1st, 3rd and 4th "_" and in front of the 2nd.

            What I am missing?

            TIA, Bigll

            6,686585
            Grand MasterGrand Master
            6,686585

              Sep 21, 2012#6

              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.

              Code: Select all

              Time Date D Zone R Zone Source Address My Address
              can be changed to

              Code: Select all

              Time Date D_Zone R_Zone Source_Address My_Address
              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_spaces.png (9.27KiB)
              Replace dialog for replacing space characters at column 12, 19, 31 and 42.

              4
              NewbieNewbie
              4

                Sep 21, 2012#7

                Thank you.
                I get it.

                :lol:

                17
                Basic UserBasic User
                17

                  Dec 20, 2012#8

                  A more generic version may be:

                  search string - "^(.{11}).(.{6}).(.{11}).(.{10})."
                  replace string - "\1_\2_\3_\4_"

                  That works no matter whether the character at the given positions is space.

                  6,686585
                  Grand MasterGrand Master
                  6,686585

                    Dec 20, 2012#9

                    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.

                    17
                    Basic UserBasic User
                    17

                      Dec 21, 2012#10

                      Thank you, Mofi, for your extended explanation.

                      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.

                      Thanks.

                        Jan 02, 2013#11

                        Now I'm facing another practical requirement:

                        How can I replace a character in specific place, counting from the end, in each line?

                        Happy New Year!

                        6,686585
                        Grand MasterGrand Master
                        6,686585

                          Jan 03, 2013#12

                          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:
                          1. With the Perl regular expression engine:

                            Find What: ;(.{4})$
                            Replace With: \t\1
                          2. 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.
                          3. With the legacy UltraEdit regular expression engine:

                            Find What: ;^(????^)$
                            Replace With: ^t^1
                          So the replace is a simple tagged regular expression replace.

                          17
                          Basic UserBasic User
                          17

                            Jan 04, 2013#13

                            Wow! It's great.

                            Thank you, Mofi.

                            Happy New Year!