AutoHotkey Community

It is currently May 27th, 2012, 3:30 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 194 posts ]  Go to page Previous  1 ... 7, 8, 9, 10, 11, 12, 13  Next
Author Message
 Post subject:
PostPosted: June 27th, 2010, 1:42 am 
Offline

Joined: August 17th, 2009, 4:59 am
Posts: 131
Thanks hugov, your suggestion worked.


Report this post
Top
 Profile  
Reply with quote  
PostPosted: July 2nd, 2010, 11:34 pm 
Offline

Joined: July 2nd, 2010, 11:25 pm
Posts: 16
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,


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 3rd, 2010, 12:52 am 
Offline
User avatar

Joined: April 4th, 2009, 8:19 pm
Posts: 1143
Location: Croatia
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 3rd, 2010, 7:58 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Seems more like a job for CSV_ModifyColumn using | as delimiter http://www.autohotkey.com/forum/topic55909.html (and I wouldn't use RegEx here). Or simply do it in Excel or other spreadsheet.

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 4th, 2010, 10:59 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
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 FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
PostPosted: July 8th, 2010, 12:28 am 
Offline

Joined: January 20th, 2007, 7:47 pm
Posts: 110
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 8th, 2010, 7:12 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
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 ... eplaceLine 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) :wink:

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Last edited by SoLong&Thx4AllTheFish on July 9th, 2010, 8:15 am, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject: THANKS
PostPosted: July 9th, 2010, 6:45 am 
Offline

Joined: January 20th, 2007, 7:47 pm
Posts: 110
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 9th, 2010, 8:14 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Thanks for pointing out my noob mistake, there goes my credibility :shock:

The examples in the documentation are based on the example test script:
http://www.autohotkey.com/forum/viewtop ... 363#280363
which should provide you with a few examples per TF Function to illustrate its use.

Further reading about variables:
http://www.autohotkey.com/docs/FAQ.htm#percent and a more elaborate one here:
http://www.autohotkey.net/~deleyd/xprxm ... amples.htm

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 9th, 2010, 9:19 am 
Offline

Joined: January 20th, 2007, 7:47 pm
Posts: 110
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 9th, 2010, 9:38 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
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/Variable ... xpressions
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 :wink:

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 4th, 2010, 4:10 pm 
Offline

Joined: April 15th, 2010, 3:15 pm
Posts: 4
Does it only support ANSI files?

How to read UTF-8 text files?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 4th, 2010, 4:26 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
I honestly don't know as I only work with the standard AutoHotkey version. You may try the unicode version of AutoHotkey_L but I don't know if the output will be a UTF-8 text if the input is a UTF-8 file. So you'll have to experiment.

If I do work with utf-8 files, I convert them to ansi as done here http://www.autohotkey.com/forum/viewtop ... 218#287218

This may also be of interest:
Transform wrote:
Performs miscellaneous math functions, bitwise operations, and tasks such as ASCII/Unicode conversion.
Source: http://www.autohotkey.com/docs/commands/Transform.htm


(If TF does not work with AHK_L I wouldn't know what to change to make it work, sorry)

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
PostPosted: December 7th, 2010, 1:46 pm 
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


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: December 7th, 2010, 2:04 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
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! <--


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 194 posts ]  Go to page Previous  1 ... 7, 8, 9, 10, 11, 12, 13  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: xXDarknessXx and 16 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group