iMacro Posted March 27, 2014 Report Share Posted March 27, 2014 ## CMP 145CMP 4 1.9788189 5.0909448 90 N R126 Flash_Parallel_256_12345567690 ;0=1,1=0.0050TOP 0 1.9788189 5.1115948 90 N 300 1 R126-1TOP 1 1.9788189 5.0702948 90 N 812 1786 R126-2## CMP 146CMP 4 1.9581688 4.9531495 180 N R125 res_Parallel_6_12345567690 ;0=1,1=0.0050TOP 0 1.9788188 4.9531495 180 N 295 1 R125-1TOP 0 1.9788188 4.9531495 180 N 295 1 R125-1 TOP 1 1.9375188 4.9531495 180 N 812 1785 R125-2 I have a 6000 line text file. I need to truncate the length of the bold strings to 20 characters. This string always starts after the 6th <space> and ends after the 7th <space> of the line, and the preceeding line always starts with "# CMP" and nowhere else in the txt file Is this doable? The resulting file would look like: ## CMP 145CMP 4 1.9788189 5.0909448 90 N R126 Flash_Parallel_256_1 ;0=1,1=0.0050TOP 0 1.9788189 5.1115948 90 N 300 1 R126-1TOP 1 1.9788189 5.0702948 90 N 812 1786 R126-2## CMP 146CMP 4 1.9581688 4.9531495 180 N R125 res_Parallel_6_12345 ;0=1,1=0.0050TOP 0 1.9788188 4.9531495 180 N 295 1 R125-1TOP 0 1.9788188 4.9531495 180 N 295 1 R125-1 TOP 1 1.9375188 4.9531495 180 N 812 1785 R125-2 Quote Link to comment Share on other sites More sharing options...
dcman98 Posted March 27, 2014 Report Share Posted March 27, 2014 First, off, I'm going to say that it is probably easier to do this in excel. But you asked about ME, so here's how I would write it. Please understand that I am not a programmer, so I design my macros to be speedy representations of what a user can do with a mouse, keyboard, and basic thinking. Looking at your data, I noticed that the section truncated starts at the 7th space, but also at the 37th character. If this is always so, it makes your life that much easier, but if not, we can still make it work. The basic operation that I would do is line by line, rewrite the text document. I'll give you the broad strokes of my design here, and if you run into trouble, I can work out some of the details. I would use the home and shift+end keystrokes to set a line of text into a variable (T1), then truncate that variable into a new variable (T2), being only the first 4 characters. If that variable then matches "# CMP", then I know that the next line is the one I want to modify. If it is not, then I want to switch to a new text document, write T1, hit enter, and repeat. It may be good to use the idea of a switch here, where if T2="# CMP", then set T99 to A, and if not, set T99 to B. Then follow the next set of commands if T99 = A, but repeat the previous if T99 = B. If it does match, I still want to write T1 into my new document, but I don't want to just repeat the process. Instead, I want it to take the next line and make the whole line a variable, then remove the first 36 characters (if this varies, then use a process of finding the first space, removing it and everything before, and repeating 6 times to get to the spot you are looking for), then find the first space, then remove that and everything after that, being sure to save the first section, the second section, and the third section in different text variables. Then shorten the 2nd section to be 20 characters, then have the program go to the new text file and write all three sections, using the now modified 2nd section. Repeat this for every line of your document, and there you have it. To see how to find the space and crop a text string using other text, see this post: http://pgmacros.invisionzone.com/topic/5126-parse-data-from-text-file/?hl=%2Bfind+%2Btext+%2Bwithin+%2Bvariable#entry22039 Let me know if you need more detail or have any questions. David Quote Link to comment Share on other sites More sharing options...
joe Posted March 28, 2014 Report Share Posted March 28, 2014 Are the lines that you want to change in your file the only ones containing a semi-colon? And if so is that semi-colon always in the same position like you have here in your examples? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.