Jump to content
Sign in to follow this  
shyang

How can I copy a specific pattern of string in each line to another file ?

Recommended Posts

shyang
Hello all
 
Recently I got to know autoit, and just a beginner.
though I searched many forum articles, I couldn't find what I want to do exactly.
So Anyone can help me please. 
 
For example, I have contents.txt file like below;
 
#cat contents.txt
asdfqgaghas
sdfqwert
dfgasdfweg 
tom is user_admin-user=12345678  testcenter1
sam is user_admin-user=12341238  testcenter2
markis user_admin-user=56234238  testcenter3
 
 
What I want to do are; 
 
- just save  "user_admin-user=########" to username.txt ,not including other strings(eg: tome is, sam is, mark is ) 
- and save "testcenter#" maching with "user_admin-user=########" to testcenter.txt
- and I want to delete all lines which don't have user_admin-user=######## string, and save as contents.after.txt
 
so Final output will be like ; 
#cat username.txt
user_admin-user=12345678
user_admin-user=12341238
user_admin-user=56234238
 
#cat testcenter.txt
testcenter1
testcenter2
testcenter3
 
#cat contents.after.txt
tom is user_admin-user=12345678  testcenter1
sam is user_admin-user=12341238  testcenter2
markis user_admin-user=56234238  testcenter3
 
 
 
I was trying to find ; 
- just copy 24characters starting from 7th character,but I'm sure there might be more efficient way to do this. 
 
Anyone can help me on this. 
 
Thanks in advance & Happy New year. 
 

Share this post


Link to post
Share on other sites
Melba23

shyang,

Not too difficult: ;)

#include <Array.au3>

; Simulate reading contents.txt into an array with FileReadToArray
Global $aLines[] = [ _
    "asdfqgaghas", _
    "sdfqwert", _
    "dfgasdfweg", _
    "tom is user_admin-user=12345678  testcenter1", _
    "sam is user_admin-user=12341238  testcenter2", _
    "markis user_admin-user=56234238  testcenter3" _
    ]

; Areate arrays large enough to hold the required data
Global $aUserName[UBound($aLines)]
Global $aTestCenter[UBound($aLines)]
Global $aAfter[UBound($aLines)]
; Create 3 counters
Global $iUserName = 0, $iTestCenter = 0, $iAfter = 0

For $i = 0 To UBound($aLines) - 1
    ; Read the line
    $sLine = $aLines[$i]
    ; Determine what to do with it
    If StringInStr($sLine, "testcenter") Then
        ; Add the required data to the relevant arrays
        $aUserName[$iUserName] = StringRegExpReplace($sLine, "^.*(user_admin-user=\d*).*$", "$1")
        $aTestCenter[$iTestCenter] = StringRegExpReplace($sLine, "^.*(testcenter\d*)$", "$1")
        ; Increase the counters
        $iUserName += 1
        $iTestCenter += 1
    Else
        ; Add line to the array
        $aAfter[$iAfter] = $sLine
        ; Increase the counter
        $iAfter += 1
    EndIf
Next

; Remove unused elements
ReDim $aUserName[$iUserName]
ReDim $aTestCenter[$iTestCenter]
ReDim $aAfter[$iAfter]

; And here is the result ready to be written to file with _FileWriteFromArray
_ArrayDisplay($aUserName, "username.txt", Default, 8)
_ArrayDisplay($aTestCenter, "testcenter.txt", Default, 8)
_ArrayDisplay($aAfter, "after.txt", Default, 8)
I hope the comments are clear, but please ask if you have any questions. :)

M23

  • Like 1

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
shyang

Thank you very much M23, 

 does this actually create output file ?

On my testing with the above script, It showed exactly what I want to do, but output files were not created.

anything I miss maybe ?

Share this post


Link to post
Share on other sites
Melba23

shyang,

 

but output files were not created

; And here is the result ready to be written to file with _FileWriteFromArray
That bit was left to you - I even gave you the name of the function to use. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
shyang

aha~!

Let me try with _FileWriteFromArray,,Thank you very much~  :D

Share this post


Link to post
Share on other sites
mikell

Melba,

Why on earth go through arrays ?

$str = "asdfqgaghas" & @crlf & _ 
    "sdfqwert" & @crlf & _ 
    "dfgasdfweg" & @crlf & _ 
    "tom is user_admin-user=12345678  testcenter1" & @crlf & _ 
    "sam is user_admin-user=12341238  testcenter2" & @crlf & _ 
    "markis user_admin-user=56234238  testcenter3"

; $str = FileRead("contents.txt")   ; <<< as enunciated 

$username =  StringRegExpReplace($str, '(?s).+?(user_admin-user=\d+)\V+', "$1" & @crlf)
msgbox(0,"", $username)
; FileWrite("username.txt", $username)  ; << ditto

$testcenter = StringRegExpReplace($str, "(?s).+?(testcenter\d+)", "$1" & @crlf)
msgbox(0,"", $testcenter)

$after = StringRegExpReplace($str, '(?m)^[^=_]+\v+', "")
msgbox(0,"", $after)
Edited by mikell

Share this post


Link to post
Share on other sites
Melba23

mikell,

 

Why on earth go through arrays ?

Because I am but a simple soul and cannot write SREs like that. ;)

M23

  • Like 1

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
mikell

Can't believe this  :)

I rather think you have too many things to think about  ;)

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.