[library] TF: Text files & Variables (strings) v3.7

Post your working scripts, libraries and tools
ahk7
Posts: 96
Joined: 06 Nov 2013, 16:35

[library] TF: Text files & Variables (strings) v3.7

10 Nov 2013, 06:16

TF v3.7 - AutoHotkey library for Text files & Variables (strings)
Library and documentation at https://github.com/hi5/TF

As the name suggest this is a AHK Library with a number of functions to "manipulate" text, both files such as *.txt, *.ahk, *.html, *.css etc AND Strings (or variables). It is NOT useful for binary files or data such as MS Office files, PDFs, EXEcutables, images etc.

By using a matchlist* for the lines you want to process for the majority of the functions it is flexible in use, but it does make it a bit slower for large files or variables. If speed is of great importance this may not be the most suitable library.

* The matchlist is generated automatically in each function based on the parameters.

Changelog v3.7

- Added: additional check for passing on just zero/zeros as text to TF functions in TF_GetData() - see v3.6
- Fix: changed readme.md to fix rendering issue of MD files on GH - https://github.com/hi5/TF/issues/5

Changelog v3.5

- Changed A_ScriptDir to A_WorkingDir in TF_ReturnOutPut - https://github.com/hi5/TF/issues/1
- Fixed: TF_ColGet negative startcolumn and sections now work correctly


Available Functions

TF
TF_CountLines
TF_Count
TF_ReadLines
TF_Tail
TF_Replace
TF_ReplaceInLines
TF_RegExReplace
TF_RegExReplaceInLines
TF_RemoveLines
TF_RemoveBlankLines
TF_RemoveDuplicateLines
TF_InsertLine
TF_ReplaceLine
TF_LineNumber
TF_InsertPrefix
TF_InsertSuffix
TF_TrimLeft
TF_TrimRight
TF_AlignLeft
TF_AlignCenter
TF_AlignRight
TF_ConCat
TF_ColGet
TF_ColPut
TF_ColCut
TF_ReverseLines
TF_Find
TF_SplitFileByLines
TF_SplitFileByText
TF_Merge
TF_Prepend
TF_Append
TF_Substract
TF_WhiteSpace
TF_Wrap
TF_RangeReplace
TF_MakeFile
TF_Tab2Spaces
TF_Spaces2Tab
TF_Sort
TF_Save
TF_Join

Keywords: FileDeleteLine Deleteline Delete Line Lines Text file duplicate remove trim split merge replace align sort wrap append prepend find column columns variable strings whitespace leading trailing reverse tail count insert read grep join
Last edited by ahk7 on 16 Apr 2017, 03:57, edited 2 times in total.
User avatar
jigga
Posts: 93
Joined: 24 Jan 2014, 00:31
Facebook: joel.fazio
Google: +JoelFazio

Re: [library] TF: Text files & Variables (strings) v3.4

09 Mar 2014, 12:51

Some of these functions can be kinda slow on big files, but this is a very useful lib :)
kiwichick
Posts: 87
Joined: 21 Jan 2014, 22:03

Re: [library] TF: Text files & Variables (strings) v3.4

18 Apr 2014, 21:18

Hi there, How would I use TF_Find on multiple files and display the results all together in one msgbox? I have this loop:

Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus


I can get:
1. results displayed in a separate msgbox for each file searched.
OR
2. results displayed in one msgbox but only for the last file in the list.

I can't figure out how to display all the results together so any help would be much appreciated :-)
ahk7
Posts: 96
Joined: 06 Nov 2013, 16:35

Re: [library] TF: Text files & Variables (strings) v3.4

19 Apr 2014, 02:08

You can "Concatenate" the results variable - see http://ahkscript.org/docs/Variables.htm#Operators

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



As a side note: TF_Find uses Regular Expressions so by its default case sensitive, so in your case if you simply type "msgbox" in the input it won't find "MsgBox" - therefore I've wrapped SearchText with some default options in the code above to save you the hassle of typing it each time "i)\Q" SearchText "\E" you can alter this to suit your needs of course. You could also return line numbers - See also https://github.com/hi5/TF#TF_Find
kiwichick
Posts: 87
Joined: 21 Jan 2014, 22:03

Re: [library] TF: Text files & Variables (strings) v3.4

19 Apr 2014, 21:17

Thanks ahk7!! I did actually manage to figure it out (along with removing duplicate results) which was a pat-on-the-back moment for me :-) I used a temporary text file but I like yours better because it removes the need for that. Cool!!

And yes it took me while to realise that the default for TF_Find was case-sensitive. Then it took me another wee while to find the code in TF.ahk to turn it off. I didn't really want it permanently turned off but wasn't sure how to tackle it and you just solved that problem for me!!! Thanks heaps!!!
ahk7
Posts: 96
Joined: 06 Nov 2013, 16:35

Re: [library] TF: Text files & Variables (strings) v3.5

03 Aug 2014, 03:32

Updated to v3.5

- Changed A_ScriptDir to A_WorkingDir in TF_ReturnOutPut - https://github.com/hi5/TF/issues/1
- Fixed: TF_ColGet negative startcolumn and sections now work correctly

https://github.com/hi5/TF
j-t-r

Re: [library] TF: Text files & Variables (strings) v3.5

10 Apr 2015, 03:14

Thanks to all previous authors and of course to you, ahk7. I use this lib very often. Hope it will be there as long as AHK itself...
ahk7
Posts: 96
Joined: 06 Nov 2013, 16:35

Re: [library] TF: Text files & Variables (strings) v3.5

10 Apr 2015, 12:16

Glad you like it - as soon as specifications for AHK v2 is are "final" I will prepare a AHK v2 compatible version :-)
j-t-r

Re: [library] TF: Text files & Variables (strings) v3.5

07 Jun 2015, 14:24

Glad to hear it. I wont be migrating to AHK2 instantly but I will be in the end. Can I have one request? Couldnt you add function to remove all the diacritics from strings? Like ěščřžýáíé etc. It means ě or é to e, š to s etc.
j-t-r
Posts: 16
Joined: 08 Jun 2015, 03:05

Re: [library] TF: Text files & Variables (strings) v3.5

08 Jun 2015, 03:22

I managed to do it by myself in the end :-) -> viewtopic.php?f=6&t=8089
Thanks.
ahk7-guest

Re: [library] TF: Text files & Variables (strings) v3.5

08 Jun 2015, 06:19

Indeed it is in principle a series of find/replace actions - you can now call your function from other TF functions, here TF reads file.txt, passes it on to your function, which in turn is passed on to TF_Save

Code: [Select all] [Download] GeSHi © Codebox Plus

TF_Save(RemoveLetterAccents(TF("file.txt")),"file.txt") 
; just to note this will create a global variable 't' - https://github.com/hi5/TF#TF
User avatar
Peace1
Posts: 41
Joined: 17 Mar 2015, 14:19
Location: 127.0.0.1

Re: [library] TF: Text files & Variables (strings) v3.5

24 Sep 2015, 15:12

Hi all,

Sorry to bump this thread but i have a little problem to manage it in my project ! I will try to explain you my goal :

I have a txt file where i want to modify values with TF_ReplaceInLines.

My text file look like something like this :

Code: [Select all] [Download] GeSHi © Codebox Plus

value1 = 15;
value2= 5000;
value3= 1000;
value4= true;
value5= false;
value6= 50;
value7= 80;
value8= 40;


I would like to modify the values by another and this is what i tried :

Code: [Select all] [Download] GeSHi © Codebox Plus

FileSelectFile, SelectedFileSettings, 3, %A_WorkingDir%, Open a file, settings.txt
FileContents := SelectedFileSettings
TF_ReplaceInLines("!FileContents", "1", "", "15", "20")


And in this way that doesn't work :(

Then if i try this code :

Code: [Select all] [Download] GeSHi © Codebox Plus

FileSelectFile, SelectedFileSettings, 3, %A_WorkingDir%, Open a file, settings.txt
FileContents := SelectedFileSettings
TF_ReplaceInLines("!" FileContents, "1", "", "15", "20")


Another error appear in the TF.ahk @line 81 : OutPut .= LoopField "`n"

Perhaps i do it bad so if someone can help me please i will appreciate it !

Edit: Well i manage it it was simply the activate#Warn sorry for disturb you guys !

Thank you in advance, regards.
Depeche_Mode
Posts: 2
Joined: 16 Nov 2015, 19:08

Re: [library] TF: Text files & Variables (strings) v3.5

25 Nov 2015, 17:49

Hello,

TF_ReplaceLine creates a new file, it does not modify the initial file.
ahk7
Posts: 96
Joined: 06 Nov 2013, 16:35

Re: [library] TF: Text files & Variables (strings) v3.5

26 Nov 2015, 14:09

@Depeche_Mode

In principle that is correct, by default all functions create a copy of the original file for safety reasons so you can first test it, only if you're sure the command works correctly you can tell TF to overwrite the source file by adding a ! in front of your file name.

You can read more about it here: (! prefix)
https://github.com/hi5/TF#quick-intro-to-parameters
https://github.com/hi5/TF#textfile-and-the--prefix
ness_1

Re: [library] TF: Text files & Variables (strings) v3.4

19 Oct 2016, 05:54

kiwichick wrote:Thanks ahk7!! I did actually manage to figure it out (along with removing duplicate results) which was a pat-on-the-back moment for me :-) I used a temporary text file but I like yours better because it removes the need for that. Cool!!

And yes it took me while to realise that the default for TF_Find was case-sensitive. Then it took me another wee while to find the code in TF.ahk to turn it off. I didn't really want it permanently turned off but wasn't sure how to tackle it and you just solved that problem for me!!! Thanks heaps!!!


Hi, how you have changed the code in TF.ahk to turn off case sensitive?
thank you
Guest

Re: [library] TF: Text files & Variables (strings) v3.5

20 Oct 2016, 02:46

@ness_1: There is no need to modify TF.ahk to "turn it off". Depending on the TF function used you can simply make use of AutoHotkey options and settings. (Regular expressions and StringCaseSense)

If you are using TF_Find for example: it uses Regular Expressions which by default is case-sensitive. If you don't want that, simply use the "i)" option.

So TF_Find("File.txt", , , "keys") will find 'keys' but not 'Keys' or 'KEYS'

If you want to make it case-insensitive use "i)":

TF_Find("File.txt", , , "i)keys")

See https://autohotkey.com/docs/misc/RegEx-QuickRef.htm
and https://github.com/hi5/TF#TF_Find (tip on literal search \Q...\E, see also a few posts above)
ness_11

Re: [library] TF: Text files & Variables (strings) v3.5

20 Oct 2016, 04:24

Guest wrote:@ness_1: There is no need to modify TF.ahk to "turn it off". Depending on the TF function used you can simply make use of AutoHotkey options and settings. (Regular expressions and StringCaseSense)

If you are using TF_Find for example: it uses Regular Expressions which by default is case-sensitive. If you don't want that, simply use the "i)" option.

So TF_Find("File.txt", , , "keys") will find 'keys' but not 'Keys' or 'KEYS'

If you want to make it case-insensitive use "i)":

TF_Find("File.txt", , , "i)keys")

See https://autohotkey.com/docs/misc/RegEx-QuickRef.htm
and https://github.com/hi5/TF#TF_Find (tip on literal search \Q...\E, see also a few posts above)


Great!! Thank you, it solve my problem
Emit
Posts: 3
Joined: 03 Mar 2017, 10:08

Re: [library] TF: Text files & Variables (strings) v3.5

03 Mar 2017, 10:17

TF_ReplaceLine replaces all lines in the file with a zero per line
Guest

Re: [library] TF: Text files & Variables (strings) v3.5

03 Mar 2017, 10:57

Most likely a syntax error or the lines you want to replace aren't there, if you have a file with three lines, don't expect it to work when you want to replace line 10 for example.

The test script below works for me:

Code: [Select all] [Download] GeSHi © Codebox Plus

; create a test file, 6 lines
Loop, 6
FileAppend, Line %A_Index%`n,TFTESTFILE-replaceline.txt

; Syntax:
; TF_ReplaceLine(Text, StartLine = 1, Endline = 0, ReplaceText = "")
TF_ReplaceLine("!TFTESTFILE-replaceline.txt",2,2,"new line 2")
TF_ReplaceLine("!TFTESTFILE-replaceline.txt",4,6,"new lines to replace lines 4 to 6")


Before:

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6

After:

Line 1
new line 2
Line 3
new lines to replace lines 4 to 6
new lines to replace lines 4 to 6
new lines to replace lines 4 to 6

If you still believe it fails, post your (test) script to replicate the problem, perhaps there is a simple solution.
Emit
Posts: 3
Joined: 03 Mar 2017, 10:08

Re: [library] TF: Text files & Variables (strings) v3.5

04 Mar 2017, 01:14

I must be out of my mind, i was:
TF_ReplaceLine("!"Var, StartLine=9, EndLine=9, ReplaceText="testline")
Thank you Guest.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Yahoo [Bot] and 17 guests