Tapatalk

Replace values in Fixed Width file column from another column

Replace values in Fixed Width file column from another column

4
NewbieNewbie
4

    Nov 03, 2008#1

    I have a fixed length text file with thousands and thousands of records. I need a script/macro to look at position 209-218, and if it contains all spaces, then replace it with the values in position 63-69. And, yes, I would need to append 3 spaces at the end of the file since I am copying a 7-character field into a 10-digit field. Can anyone help?

    Thanks!

    6,685587
    Grand MasterGrand Master
    6,685587

      Nov 03, 2008#2

      Is your file a text file or a binary file?

      Does your file have lines with a fixed length for all lines with a line ending character or is it a file with a huge number of bytes without line endings?

      That is not 100% clearly explained in your post. It would be very helpful for us if you follow the recommendations in the readme announcement and post also the version of UE you use, the regular expression engine you prefer, a small test input file which shows the content before and a small output file after running the regular expression replace, script or macro, best packed with ZIP or RAR and attached to your post.
      Best regards from an UC/UE/UES for Windows user from Austria

      4
      NewbieNewbie
      4

        Nov 03, 2008#3

        Sorry about the lack of info in my original post. Here is some clarification:

        -It is a text file (see attached).
        -The data contain lines with a fixed length for ALL lines.
        -There is an ending character (CRLF) at the end of every record.
        -UE Version is 13.20a+1.
        -Regular Expression engines preferred: UltraEdit style is fine, if that is easier to implement. This is so new to me that I have no preference yet.
        -I've attached a zip file that contains a Before script snapshot and an After script snapshot - deleted later.

        Thanks.

        262
        MasterMaster
        262

          Nov 03, 2008#4

          Maybe Mofi has signed off for tonight ;-) - here's is how:

          Use
          Search - Replace in files...

          Find what:
          ^(.{62}(.{7}).{139})[ ]{10}

          Replace:
          \1\2...
          (the three dots should be three spaces)

          In files type: (specify your file mask)
          directory: (specify your path).

          Use "Regular expressions" and switch on perl regular expression engine (check with UE help to find out how).

          Be careful!! - this will alter multiple files on disk of setup wrong - use at own risk !!

          6,685587
          Grand MasterGrand Master
          6,685587

            Nov 04, 2008#5

            Thanks jorrasdk, I thought about exactly the same Perl regular expression if

            the file is a text file,
            has lines and
            version of UE used is v12.00 or any later.

            The [ ] can be replaced by just a single space in the search string. But the rest of the search string is perfect.
            Best regards from an UC/UE/UES for Windows user from Austria

            4
            NewbieNewbie
            4

              Nov 05, 2008#6

              Jorrasdk, Mofi,

              Awesome! That worked. For some reason, I couldn't get it working in ver13, but I just upgraded to 14.20 and it worked there. Thanks so much.

              Can I ask for a little explanation on why that worked (or point me to some documentation)? Most of it makes sense, but I'm a little confused on what the 62 and 139 represent. I assumed they were starting positions in my file, but my file has position 63 and 209 as the starting positions for my data elements.

              Thanks again.

              262
              MasterMaster
              262

                Nov 05, 2008#7

                Here is an explanation made using RegexBuddy - also check www.regular-expressions.info for tutorials, references and more.

                ^(.{62}(.{7}).{139}) {10}

                Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
                Match the regular expression below and capture its match into backreference number 1 «(.{62}(.{7}).{139})»
                --Match any single character that is not a line break character «.{62}»
                ----Exactly 62 times «{62}»
                --Match the regular expression below and capture its match into backreference number 2 «(.{7})»
                ----Match any single character that is not a line break character «.{7}»
                ------Exactly 7 times «{7}»
                --Match any single character that is not a line break character «.{139}»
                ----Exactly 139 times «{139}»
                Match the space character « {10}»
                --Exactly 10 times «{10}»

                So you see 62 and 139 are number of columns to skip past to get to the "interesting" columns.

                4
                NewbieNewbie
                4

                  Nov 06, 2008#8

                  Thank you, jorrasdk. Very helpful.