Every day I have to open a fixed length text file. Highlight a range of columns (like column 10 - 20) copy the selected columns and paste in new text file.
What would be the best way to automate this process? Script? Macro?
The record length (amount of lines) changes daily. One day there may be 100 lines, the next day it could be 10,0000.
I would love to be able to just hit a hot key and it does this for me. I do not think a macro will help this issue, because the record length changes from day to day.
Any help is appreciated.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
this can be done with a simple Python script.
So, install Python plugin and create a script with the following content:
~~~~~~~~~~~~~~~~~~
import os
START = 3
END = 10
base, ext = os.path.splitext(notepad.getCurrentFilename())
text = editor.getText()
notepad.new()
for line in text.splitlines():
editor.appendText(line[START:END] + "\n")
notepad.saveAs(base + "_column_extracted" + ext)
~~~~~~~~~~~~~~~
.
Obviously, adjust START ad END variables as needed. It these values change everyday, script can be adjusted to start with a simple message box prompt.
If you can't figure out how to assign keyboard shortcut to run this script, ask here for further instructions.
BR,
Loreia
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm sure that I'll tease Loreia2 if I say that you can also achieve what you want with the help of a search/replacement, in regular expression mode. And I must admit that Loreia2's python script is not very difficult to understand, even if you don't know anything about Python language :-)
Don't worry Loreai2 : Next year, I promise you that I'll learn basic python language and scripts ! BTW, the Python plugin is already installed on my configuration.
Well, Jason, let go back to your problem.
First of all, from the test text below, with the first line, before the line a, empty :
a
ab
abc
abcd
abcde
abcdef
abcdefg
abcdefgh
abcdefghi
abcdefghij
abcdefghijk
abcdefghijkl
abcdefghijklm
abcdefghijklmn
Let suppose you would like extract, from the text above, all the characters, from the 4th character till the 10th character ( in other words : only 7 characters from the 4th, with the first 3 characters ignored )
Which kind of result do you expect to ( case A, B, C or D ) ?
A)
a
ab
abc
d
de
def
defg
defgh
defghi
defghij
defghij
defghij
defghij
defghij
B)
a
ab
abc
abcd
abcde
abcdef
abcdefg
abcdefgh
abcdefghi
defghij
defghij
defghij
defghij
defghij
C)
d
de
def
defg
defgh
defghi
defghij
defghij
defghij
defghij
defghij
D)
defghij
defghij
defghij
defghij
defghij
So, in addition to the main rule, noted above :
In case A, all the lines, with length up to 3 characters, even blank lines, are kept, without any change.
In case B, all the lines, with ** length up to 9 characters ( 3 + 7 - 1 ), even blank lines, are kept, without any** change.
In case C, all the lines with length up to 3 characters are completely deleted, blank lines included.
In case D, all the lines, with length up to 9 characters ( 3+ 7 - 1 ), are completely deleted, blank lines included.
So, let call :
the first column, you would like to extract to, with letter s ( for start ) => s = 4, in our example.
the last column with letter e ( for end ) => e = 10, in our example.
In the two last S/R, when the first part of the alternative is matched ( lines of length less than s or e characters ) => Group 1, in second part of the alternative, is, obviously, NOT defined. Thus, in replacement, the null string is supposed, as Group 1 =\1 is re-written ONLY IF group 1 exists ! )
As I exposed a general method, it may looks a bit difficult, but remember that you just need one regex only, among these four ones !
Best regards,
guy038
Last edit: THEVENOT Guy 2013-12-04
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Every day I have to open a fixed length text file. Highlight a range of columns (like column 10 - 20) copy the selected columns and paste in new text file.
What would be the best way to automate this process? Script? Macro?
The record length (amount of lines) changes daily. One day there may be 100 lines, the next day it could be 10,0000.
I would love to be able to just hit a hot key and it does this for me. I do not think a macro will help this issue, because the record length changes from day to day.
Any help is appreciated.
Hi Jason,
this can be done with a simple Python script.
So, install Python plugin and create a script with the following content:
~~~~~~~~~~~~~~~~~~
import os
START = 3
END = 10
base, ext = os.path.splitext(notepad.getCurrentFilename())
text = editor.getText()
notepad.new()
for line in text.splitlines():
editor.appendText(line[START:END] + "\n")
notepad.saveAs(base + "_column_extracted" + ext)
~~~~~~~~~~~~~~~
.
Obviously, adjust START ad END variables as needed. It these values change everyday, script can be adjusted to start with a simple message box prompt.
If you can't figure out how to assign keyboard shortcut to run this script, ask here for further instructions.
BR,
Loreia
Hi, Jason,
I'm sure that I'll tease Loreia2 if I say that you can also achieve what you want with the help of a search/replacement, in regular expression mode. And I must admit that Loreia2's python script is not very difficult to understand, even if you don't know anything about Python language :-)
Don't worry Loreai2 : Next year, I promise you that I'll learn basic python language and scripts ! BTW, the Python plugin is already installed on my configuration.
Well, Jason, let go back to your problem.
First of all, from the test text below, with the first line, before the line a, empty :
a
ab
abc
abcd
abcde
abcdef
abcdefg
abcdefgh
abcdefghi
abcdefghij
abcdefghijk
abcdefghijkl
abcdefghijklm
abcdefghijklmn
Let suppose you would like extract, from the text above, all the characters, from the 4th character till the 10th character ( in other words : only 7 characters from the 4th, with the first 3 characters ignored )
Which kind of result do you expect to ( case A, B, C or D ) ?
A)
a
ab
abc
d
de
def
defg
defgh
defghi
defghij
defghij
defghij
defghij
defghij
B)
a
ab
abc
abcd
abcde
abcdef
abcdefg
abcdefgh
abcdefghi
defghij
defghij
defghij
defghij
defghij
C)
d
de
def
defg
defgh
defghi
defghij
defghij
defghij
defghij
defghij
D)
defghij
defghij
defghij
defghij
defghij
So, in addition to the main rule, noted above :
In case A, all the lines, with length up to 3 characters, even blank lines, are kept, without any change.
In case B, all the lines, with ** length up to 9 characters ( 3 + 7 - 1 ), even blank lines, are kept, without any** change.
In case C, all the lines with length up to 3 characters are completely deleted, blank lines included.
In case D, all the lines, with length up to 9 characters ( 3+ 7 - 1 ), are completely deleted, blank lines included.
So, let call :
the first column, you would like to extract to, with letter s ( for start ) => s = 4, in our example.
the last column with letter e ( for end ) => e = 10, in our example.
Then :
SEARCH :
^.{s-1}(.{1,e-s+1}).*and REPLACE :\1=>SEARCH :
.{3}(.{1,7}).*SEARCH :
^.{s-1}(.{e-s+1}).*and REPLACE :\1=>SEARCH :
.{3}(.{7}).*SEARCH :
^.{0,s-1}\R|^.{s-1}(.{1,e-s+1}).*and REPLACE :?1\1=> SEARCH :^.{0,3}\R|^.{3}(.{1,7}).*SEARCH :
^.{0,e-1}\R|^.{s-1}(.{e-s+1}).*and REPLACE :?1\1=> SEARCH :^.{0,9}\R|^.{3}(.{7}).*NOTE :
In the two last S/R, when the first part of the alternative is matched ( lines of length less than s or e characters ) => Group 1, in second part of the alternative, is, obviously, NOT defined. Thus, in replacement, the null string is supposed, as Group 1 =\1 is re-written ONLY IF group 1 exists ! )
As I exposed a general method, it may looks a bit difficult, but remember that you just need one regex only, among these four ones !
Best regards,
guy038
Last edit: THEVENOT Guy 2013-12-04