Replace values in Fixed Width file column from another column

Find, replace, find in files, replace in files, regular expressions

Replace values in Fixed Width file column from another column

Postby skerper » Mon Nov 03, 2008 1:23 pm

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!
skerper
Newbie
 
Posts: 4
Joined: Mon Nov 03, 2008 1:06 pm

Re: Replace values in Fixed Width file column from another column

Postby Mofi » Mon Nov 03, 2008 2:43 pm

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.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4041
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Replace values in Fixed Width file column from another column

Postby skerper » Mon Nov 03, 2008 3:24 pm

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.
skerper
Newbie
 
Posts: 4
Joined: Mon Nov 03, 2008 1:06 pm

Re: Replace values in Fixed Width file column from another column

Postby jorrasdk » Mon Nov 03, 2008 3:47 pm

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 !!
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: Replace values in Fixed Width file column from another column

Postby Mofi » Tue Nov 04, 2008 9:27 am

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.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4041
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Replace values in Fixed Width file column from another column

Postby skerper » Wed Nov 05, 2008 9:41 am

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.
skerper
Newbie
 
Posts: 4
Joined: Mon Nov 03, 2008 1:06 pm

Re: Replace values in Fixed Width file column from another column

Postby jorrasdk » Wed Nov 05, 2008 10:41 am

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.
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: Replace values in Fixed Width file column from another column

Postby skerper » Thu Nov 06, 2008 6:49 am

Thank you, jorrasdk. Very helpful.
skerper
Newbie
 
Posts: 4
Joined: Mon Nov 03, 2008 1:06 pm


Return to Find/Replace/Regular Expressions