Jump to content

Find string Copy text


Recommended Posts

Can someone point me in the right direction please.   I have a text find that looks like this:

01|  varying amount of data

02 | varying amount of data

03 | varying amount of data

04|  varying amount of data

05 | varying amount of data

06 | varying amount of data

07|  varying amount of data

08 | varying amount of data

09 | varying amount of data

10|  varying amount of data

11 | varying amount of data 

Repeats until end of file.  The amount of lines vary between  01  and  13.    Each is a record that starts with the 01 line of text.

On line 02  I have a date  YYMMDD.   This is the string I will search for.   Once this string is found I want to copy that record  Lines 01 - until next 01

I have no problem finding the string.   But how do I select this record (lines 01 - until next 01)  once the string is found?  

An example would be great but just a point in the right direction would be good.

Thank you

Link to comment
Share on other sites

Can you provide an example text file?  Also what does "Once this string is found I want to copy that record Lines 01 - until next 01"?  I would suggest _FileReadToArray Then _ArraySearch or _ArrayFindAll then you can specify what row to copy, but unsure exactly what information you want to retain/replace?

Link to comment
Share on other sites

$sourcearray = stringsplit on @CR

$all_01_lines = _ArrayFindAll  the 01 line of texts in the $sourcearray

then you can search the $sourcearray[$all_01_lines[$i]] to $sourcearray[$all_01_lines[$i+1] for the matching string and take just that group if it exists

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

here is my thought in code form, with an example text that I shat

string.txt

01LINE
000101
apples
bananas
01LINE
010101
cat
dog
pig
01LINE
020101
car
airplane
boat
01LINE
030101
knife
gun
artillery
#include<array.au3>

$aArr = stringsplit(fileread("string.txt") , @CRLF , 3)
$aFind = _ArrayFindAll($aArr , "01LINE" , 0 , 0 , 0 , 2)

;~ $date = "000101"
;~ $date = "010101"
$date = "020101"
;~ $date = "030101"

For $i = 0 to ubound($aFind) - 1

    $search = $i = ubound($aFind) - 1 ? _ArraySearch($aArr , $date , $aFind[$i]) : _ArraySearch($aArr , $date , $aFind[$i] , $aFind[$i + 1] , 0 , 1)

        If $search <> -1 then
            $sOut = $i < ubound($aFind) - 1 ?   _ArrayToString($aArr , @CR , $aFind[$i] + 1 , $aFind[$i + 1] - 1) : _ArrayToString($aArr , @CR , $aFind[$i] + 1 , -1)
            msgbox(0, '' , $sOut)
        EndIf

next

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

im so proud, that's a much better split (if my text ends up being similar to the OPs)

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

2 hours ago, mikell said:

Please note that this doesn't use regex  :)

Since you mentioned it...

$date = "020101"
local $sre = stringregexp(fileread(@scriptdir & "\string.txt"),'(?ims).*?(' & $date & '.*?)01line',3)
if isarray($sre) then msgbox(0,'Ha...Ha Mikell',$sre[0])

 

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...