AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Simple Script to Search and Copy adjacent Text.

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Strickt1
Guest





PostPosted: Tue Feb 16, 2010 8:54 pm    Post subject: Simple Script to Search and Copy adjacent Text. Reply with quote

I have a text file which has quite a bit of text that I have to check on a regular basis. I was wondering if there was a way of using autohotkey to create a script that will search the text for a specific word and then copy the text that is right next to it?

I know the basics of scripting and have gone through the tutorials, and I think I would be able to get the copied data where I need to use it.

For example I would like to be able to find and copy to clipboard the license # from the following example,


Quote:
Dog license number registered to Jane Doe
Burmington California
2 warnings given, has following dog tags recorded.

Jane Doe, Dog Name Scruffy, License#1 abcd1234
123 roadname st.
Burmington, CA. 33124

Record # 92211LG


I want to create a script that would search out "License#1" but then copy and paste the actual license # listed just to the right of that, in this example it's abcd1234. Because the license number will always change I don't think I can have it search for that, I need to have this script search for License#1 as that doesn't change, but it is the license number adjacent to that (abcd1234) that I DO need copied to clipboard.

Thank you very much for any help[/quote]
Back to top
None



Joined: 28 Nov 2009
Posts: 3086

PostPosted: Tue Feb 16, 2010 9:41 pm    Post subject: Reply with quote

You'll want StringGetPos to find "Record #" and StringMid to get the text after it.
How you get the text into a variable for autohotkey to search it is dependant on how you access it.
FileRead can read the contents of a text file.
ControlGetText would be easiest if you already have the file open in Notepad.
Back to top
View user's profile Send private message
sinkfaze



Joined: 18 Mar 2008
Posts: 5043
Location: the tunnel(?=light)

PostPosted: Tue Feb 16, 2010 9:58 pm    Post subject: Reply with quote

Probably the easiest way to do that would be to use regex since the standard string manipulation functions don't handle vertical whitespace well:

Code:
var=
(
Dog license number registered to Jane Doe
Burmington California
2 warnings given, has following dog tags recorded.

Jane Doe, Dog Name Scruffy, License#1 abcd1234
123 roadname st.
Burmington, CA. 33124

Record # 92211LG
)
RegExMatch(var,"is)(?<=License#1 )\S+",m) ; will match any non-whitespace characters preceded by "License#1 "
MsgBox % m
return

_________________
Try Quick Search for Autohotkey or see the tutorial for newbies.
Back to top
View user's profile Send private message Send e-mail
Leef_me



Joined: 08 Apr 2009
Posts: 5336
Location: San Diego, California

PostPosted: Tue Feb 16, 2010 10:01 pm    Post subject: Reply with quote

Saying the same things as others, just slower to post Embarassed

May I suggest looking at the following commands:

search the text for a specific word
http://www.autohotkey.com/docs/commands/StringGetPos.htm

if you just want to see if the word is in a varable
http://www.autohotkey.com/docs/commands/IfInString.htm

copy the text that is right next to it
http://www.autohotkey.com/docs/commands/StringMid.htm

you might need this to determine the length of the 'word'
or you could just count and put 10 in a variable 'License#1 ' (including the trailing space).
http://www.autohotkey.com/docs/commands/StringLen.htm

I DO need copied to clipboard
http://www.autohotkey.com/docs/misc/Clipboard.htm

You don't describe how you plan to read the file.
Autohotkey has various methods to read a file, here is the command list
http://www.autohotkey.com/docs/commands.htm

This command would probably work the easiest, though maybe no the best. It reads a line of text and if License# and the actualt number are on the same text line the above commands would easily allow you to copy the text from the "#1" to the end of the line.
http://www.autohotkey.com/docs/commands/FileReadLine.htm
Back to top
View user's profile Send private message
poo_noo



Joined: 08 Dec 2006
Posts: 248
Location: Sydney Australia

PostPosted: Tue Feb 16, 2010 10:02 pm    Post subject: Reply with quote

You guys are too quick for me

heres my attempt
Code:
theData =
(
Dog license number registered to Jane Doe
Burmington California
2 warnings given, has following dog tags recorded.

Jane Doe, Dog Name Scruffy, License#1 abcd1234
123 roadname st.
Burmington, CA. 33124

Record # 92211LG
Jane Doe, Dog Name Dog2, License#1 TEST1234
Jane Doe, Dog Name Scruffy3, License#1 Test12345
Jane Doe, Dog Name Scruffyddd, License#1 abcd12346
)

; read the data.
Loop, parse, theData, `n, `r  ; Specifying `n prior to `r allows both Windows and Unix files to be parsed.
   {
   ; skip blank lines
   If A_LoopField =
      Continue
   ; look for the line that has License#1 in it
   IfInString, A_LoopField, License#1
      {
      ; get the licence number by RegExReplace & stored in variable 'licences'
      licences .= RegExReplace(A_LoopField, "^.*License#1 (.*)", "$1") . "`n"
      }
   }
msgbox outta loop`n`n%licences%

_________________
Paul O
Back to top
View user's profile Send private message Visit poster's website
Strickt1
Guest





PostPosted: Thu Feb 18, 2010 1:41 am    Post subject: Thank you guys very much Reply with quote

Thanks so much for your assistance, I will research and put these methods to use and let you know how it goes. I'm still learning so my followup may be some time out but atleast I have somewhere to start, and some example code to work with. Thank you!
Back to top
Strickt1
Guest





PostPosted: Thu Feb 18, 2010 1:55 am    Post subject: O man Reply with quote

I can't thank you enough, you have no idea how much easier this will make my work once I got this put together. I know it's possible now, I just need to configure it.

Thanks again,
Back to top
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group