 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
stressbaby
Joined: 17 Aug 2009 Posts: 131
|
Posted: Sun Jun 27, 2010 12:42 am Post subject: |
|
|
| Thanks hugov, your suggestion worked. |
|
| Back to top |
|
 |
nnah
Joined: 02 Jul 2010 Posts: 16
|
Posted: Fri Jul 02, 2010 10:34 pm Post subject: Replace string in file A with string |
|
|
I am new to Auto hot key, so please bear with me, I only know the basic right now.
I have a file which has the below information. We will call File A
20100517|L00015099|97001GP|1||||||
20100517|L00014977|97001GP|1||||||
20100517|L00015024|97001GP|1||||||
20100517|L00015081|97530GP|4||||||
20100517|L00014996|97110GP|2||||||
20100517|L00014996|97530GP|1||||||
I need to replace all the information in the second column ex (L00015099) with data from a sample file below which we will call File B
4200010
4200012
4200013
4200020
4200060
4200068
Can someone please start me off...I have this huge file that I need to manipulate and upload into the system before the weekend is over....I really need help.
Thanks, |
|
| Back to top |
|
 |
Learning one
Joined: 04 Apr 2009 Posts: 1001 Location: Croatia
|
Posted: Fri Jul 02, 2010 11:52 pm Post subject: |
|
|
Well, if it's urgent - here you are;
| Code: | FileA=
(
20100517|L00015099|97001GP|1||||||
20100517|L00014977|97001GP|1||||||
20100517|L00015024|97001GP|1||||||
20100517|L00015081|97530GP|4||||||
20100517|L00014996|97110GP|2||||||
20100517|L00014996|97530GP|1||||||
)
FileB=
(
4200010
4200012
4200013
4200020
4200060
4200068
)
MsgBox % nnahReplace(FileA,FileB)
nnahReplace(FileA,FileB) {
StringSplit, b, FileB, `n
Loop, parse, FileA, `n
Replace .= RegExReplace(A_LoopField,"(\d*\|)(\w*\|)(.*)","$1" b%A_Index% "|$3") "`n"
if (SubStr(Replace,0) = "`n")
StringTrimRight, Replace, Replace, 1
return Replace
} |
P.S. I'll probably be offline in next 48 hours, so if something is wrong, I won't be able to help you. And btw, this is for Ask for Help forum. |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
Posted: Sun Jul 04, 2010 9:59 am Post subject: |
|
|
As proof of concept, you COULD use TF in this case because of the fixed width of the columns (with varying widths of columns you wouldn't succeed, at least not until I add a TF_FixedCol function ;-))
| Code: | a= ; test data
(
20100517|L00015099|97001GP|1||||||
20100517|L00014977|97001GP|1||||||
20100517|L00015024|97001GP|1||||||
20100517|L00015081|97530GP|4||||||
20100517|L00014996|97110GP|2||||||
20100517|L00014996|97530GP|1||||||
)
b= ; test data
(
4200010
4200042
4200066
4200166
4200001
4200111
)
Col1:=TF_ColGet(a, 1, 0, 1, 9) ; store first column in var
Col3toEnd:=TF_ColGet(a, 1, 0, 9, 100000) store column 3+ in var
NewA:=TF_ConCat(Col1,b) ; join column1 and new column2 (b)
NewA:=TF_ConCat(NewA,Col3toEnd) ; join new col1+2 and column 3+
MsgBox % NewA ; result |
_________________ AHK Wiki FAQ
TF : Text files & strings lib, TF Forum |
|
| Back to top |
|
 |
EdScriptNewbie
Joined: 20 Jan 2007 Posts: 110
|
Posted: Wed Jul 07, 2010 11:28 pm Post subject: Line Text: TF_CountLines(Text) Error: Duplicate function |
|
|
In hopes of DateStamping the first line of a frequently updated file, I attempted to use your library as follows:
| Code: | #t::
FileToStamp = temp1.txt
FileToStampPath = C:\Program Files\AutoHotkey\Scripts-Ed\REF_SCRIPTS_TARGETS_AND_INI'S\
gosub,DateStampFileLine1
sleep sleeplong
run,%FileToStamp%,%FileToStampPath%
return
DateStampFileLine1: ; Before calling: close FileToStamp, and assign FileToStamp & FileToStampPath
; 1. make current DateStamp.txt...
DateStampString = %A_Year%-%A_Mon%-%A_MDay%{space}%A_Hour%:%A_Min%:%A_Sec%
Filedelete,C:\Program Files\AutoHotkey\Scripts-Ed\REF_SCRIPTS_TARGETS_AND_INI'S\DateStampFile.txt
Fileappend,%TimeStampString%,C:\Program Files\AutoHotkey\Scripts-Ed\REF_SCRIPTS_TARGETS_AND_INI'S\DateStampFile.txt
; 2. delete FileToStamp's old date stamp...
#include C:\Program Files\AutoHotkey\Scripts-Ed\REF_SCRIPTS_TARGETS_AND_INI'S\TF_Lib.ahk
TF_RemoveLines(%FileToStamp%,"1")
; 3. put DateStampString at start of FileToStamp...
#includeAgain C:\Program Files\AutoHotkey\Scripts-Ed\REF_SCRIPTS_TARGETS_AND_INI'S\TF_Lib.ahk
TF_Prepend(DateStampFile.txt,%FileToStamp%) ; Prepends Date File to FileToStamp
return
| I got the following error.
Error at line 47 in #include file "C:\Program Files\AutoHotkey\Scripts-Ed\REF_SCRIPTS_TARGETS_AND_INI'S\TF_Lib.ahk".
Line Text: TF_CountLines(Text) Error: Duplicate function definition.
Though I'm sure you guessed, line 47in the library as I've saved it is as follows | Code: | TF_CountLines(Text)
{
TF_GetData(OW, Text, FileName)
StringReplace, Text, Text, `n, `n, UseErrorLevel
Return ErrorLevel + 1
}
| What am I doing wrong?
THANKS! _________________ ...Ed |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
Posted: Thu Jul 08, 2010 6:12 am Post subject: |
|
|
The problem is marked in the red lines | Code: | DateStampFileLine1: ; Before calling: close FileToStamp, and assign FileToStamp & FileToStampPath
; 1. make current DateStamp.txt...
DateStampString = %A_Year%-%A_Mon%-%A_MDay%{space}%A_Hour%:%A_Min%:%A_Sec%
Filedelete,C:\Program Files\AutoHotkey\Scripts-Ed\REF_SCRIPTS_TARGETS_AND_INI'S\DateStampFile.txt
Fileappend,%TimeStampString%,C:\Program Files\AutoHotkey\Scripts-Ed\REF_SCRIPTS_TARGETS_AND_INI'S\DateStampFile.txt
; 2. delete FileToStamp's old date stamp...
#include C:\Program Files\AutoHotkey\Scripts-Ed\REF_SCRIPTS_TARGETS_AND_INI'S\TF_Lib.ahk
TF_RemoveLines(%FileToStamp%,"1")
; 3. put DateStampString at start of FileToStamp...
#includeAgain C:\Program Files\AutoHotkey\Scripts-Ed\REF_SCRIPTS_TARGETS_AND_INI'S\TF_Lib.ahk
TF_Prepend(DateStampFile.txt,%FileToStamp%) ; Prepends Date File to FileToStamp
return |
To use TF you can either place it a LIB directory in the folder where autohotkey.exe is, e.g. c:\program files\autohotkey\lib, if there is no lib folder yet make one and put tf.ahk in it. The second option is to use use #include tf.ahk at the very top or bottom of the script but you only need it once, it seems like you try to include multiple times so the duplication error pops up as you have shown.
Rather than two separate functions TF_Remove and TF_Prepend you can use http://www.autohotkey.net/~hugov/tf-lib.htm#TF_ReplaceLine like so
| Code: | | TF_ReplaceLine("!" . FileToStampPath . FileToStamp, 1, 1, DateStampString) |
So your entire script should be something like this:
| Code: | #Include tf.ahk ; or comment line if you place TF in your LIB. Only include once
#t::
FileToStamp = temp1.txt
FileToStampPath = C:\Program Files\AutoHotkey\Scripts-Ed\REF_SCRIPTS_TARGETS_AND_INI'S\
Gosub,DateStampFileLine1
Sleep sleeplong
Run,%FileToStamp%,%FileToStampPath%
Return
DateStampFileLine1: ; Before calling: close FileToStamp, and assign FileToStamp & FileToStampPath
; 1. make current DateStamp.txt...
DateStampString = %A_Year%-%A_Mon%-%A_MDay%{space}%A_Hour%:%A_Min%:%A_Sec%
Filedelete,C:\Program Files\AutoHotkey\Scripts-Ed\REF_SCRIPTS_TARGETS_AND_INI'S\DateStampFile.txt
Fileappend,%TimeStampString%,C:\Program Files\AutoHotkey\Scripts-Ed\REF_SCRIPTS_TARGETS_AND_INI'S\DateStampFile.txt
TF_ReplaceLine("!" . FileToStampPath . FileToStamp, 1, 1, DateStampString)
Return |
Edit: corrected the ! typo (note to self, don't edit code in browser)  _________________ AHK Wiki FAQ
TF : Text files & strings lib, TF Forum
Last edited by SoLong&Thx4AllTheFish on Fri Jul 09, 2010 7:15 am; edited 2 times in total |
|
| Back to top |
|
 |
EdScriptNewbie
Joined: 20 Jan 2007 Posts: 110
|
Posted: Fri Jul 09, 2010 5:45 am Post subject: THANKS |
|
|
I couldn't make | Code: | | TF_ReplaceLine(! . FileToStampPath . FileToStamp, 1, 1, DateStampString) |
work, I'm not sure why, but once I put the file and path into one variable and the ! in ""'s it worked great.
| Code: | | TF_ReplaceLine("!"FileToStampWithPath, 1, 1, DateStampString ) |
I love this library and can see other things I'd like to try with it. It also made me realize my help file must be old b/c I didn't have the http://www.autohotkey.com/docs/Functions.htm#lib in it, so the Lib stuff is all new to me, and thanks for it.
I'm also finding difficulties with syntax, and did about a dozen versions of the above line before finding something that would work. I'm beginning to see some patterns but when to use and when not to use pairs of things like "" %% {} and () still often baffles me. Plus some of the examples I find, they only use one where I would have thought there'd be a pair. I'll figure things out eventually; if you have any suggestions of things to read, I'd be grateful.
Thanks again! _________________ ...Ed |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
|
| Back to top |
|
 |
EdScriptNewbie
Joined: 20 Jan 2007 Posts: 110
|
Posted: Fri Jul 09, 2010 8:19 am Post subject: |
|
|
Trust me, you're way credible still.
I'm feeling my way along, so it doesn't take much to throw me off, and the TF test script seemed to assume somebody more programming-knowledgeable than I. Even little things like
msgbox % myvar
I'd _never_ seen that syntax before and couldn't find it anyplace in the docs. Where do people learn these tricks? I admire and respect that programmers pride themselves in writing the shortest possible code and the most terse comments, but I spend time under water trying to figure it out. The examples seem to have a lot of one-letter variables that seem more like parameters, and multiple different lines with identical comments. or
TF_InsertSuffix("File.txt","2-4,6-9","", " Hello") ; Suffix Hello in lines 2 to 4 and 6 to 9
--> ok, why does it show ,"2-4,6-9", and not ,"2-4","6-9", ...?
Duh stuff to those who know, but I'm not there yet. But enough whinging, as the brits like to say; I _really_ like the expression examples and in five mins learned quite a bit. Also it felt good to see that somebody else finds some of this stuff non-obvious.
Thanks again. I really appreciate the help. _________________ ...Ed |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
Posted: Fri Jul 09, 2010 8:38 am Post subject: |
|
|
| Quote: | TF_InsertSuffix("File.txt","2-4,6-9","", " Hello") ; Suffix Hello in lines 2 to 4 and 6 to 9
--> ok, why does it show ,"2-4,6-9", and not ,"2-4","6-9", ...? |
To explain:
"2-4,6-9" means: Lines 2-3-4 and 6-7-8-9
The examples mainly serve to illustrate the use of the startline parameter which makes TF flexible as you can operate of various sections of a file and not just between a start and end line.
"2-4","6-9" would translate as: startline = 2-3-4 and endline = 6-7-8-9, BUT endline is always ONE specific line number not a section or incremental value. So "2-4","6-9" would be wrong.
The endline parameter can not have - + or , in it. Just a number.
See http://www.autohotkey.net/~hugov/tf-lib.htm#StartLine
Your "MsgBox % ..." question:
http://www.autohotkey.com/docs/Variables.htm#Expressions
The % forces an expression ... so rather than "%var%" you can write "% var".
I might add more basic examples to the documentation - most of the extra info in there is already based on user feedback and FAQs  _________________ AHK Wiki FAQ
TF : Text files & strings lib, TF Forum |
|
| Back to top |
|
 |
shyangs
Joined: 15 Apr 2010 Posts: 4
|
Posted: Wed Aug 04, 2010 3:10 pm Post subject: |
|
|
Does it only support ANSI files?
How to read UTF-8 text files? |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
|
| Back to top |
|
 |
cliendo Guest
|
Posted: Tue Dec 07, 2010 12:46 pm Post subject: Trying to use TF_RegExReplace but doesn't do what I expected |
|
|
I have the following text:
[30]: 11
[31]: 7
[32]: 25
And I am trying to use TF_RegExReplace() to get rid of the text from ":" till CR+LF and replace them by a comma (,). The purpose is to obtain:
[30],[31],[32]
I couldn't find any PCRE related structure to work out to get the desired outcome. I would like to get rid of the CR+LF at the end so I could have the [30], [31],[32] in the same row.
I couldn't even get rid of the CR+LF at the end of every row.
Could you give me and advice about how to do it?
Rgds,
CL |
|
| Back to top |
|
 |
Tuncay
Joined: 07 Nov 2006 Posts: 1886 Location: Germany
|
Posted: Tue Dec 07, 2010 1:04 pm Post subject: |
|
|
I am not working with the TF-library, so can not provide help on it. But I have here an alternative, however it is not a regex-only solution.
| Code: | #NoEnv
SendMode, Input
SetWorkingDir %A_ScriptDir%
text =
(
[30]: 11
[31]: 7
[32]: 25
)
text := RegExReplace(text, "`am)(*BSR_ANYCRLF):.*$", ",")
StringReplace, text, text, `r,, All
StringReplace, text, text, `n,, All
StringTrimRight, text, text, 1 ; last comma
MsgBox %text%
|
_________________ {1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <-- |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|