Sign in to follow this  
Followers 0
Albertxu

How can I move a certain line(s) of text up in a txt file?

6 posts in this topic

I had just solved a problem, but here comes another. I have a word list with explanation and phonetic. The pattern is like:

+wary
#adj."here is Chinese translation"
&ˈwɛəri
$3
+welter
#n."here is Chinese translation"
#vi."here is Chinese translation"
&ˈweltə
$3
+whimsy
#n."here is Chinese translation"
&ˈhwɪmzi:
$3

You can see from the pattern that the first line is the word entry, the second or third line is the Chinese translation, the last to second line is the phonetic sound, which needs to be moved to the position close to the word entry. And the last line is all the same, which is $3.

Now here comes the task, I want to change the pattern to:

+wary &ˈwɛəri
#adj."here is Chinese explanation"

+welter &ˈweltə
#n."here is Chinese explanation"
#vi."here is Chinese explanation"

+whimsy &ˈhwɪmzi:
#n."here is Chinese explanation"

I know this can be done with VBA script, but since I am learning AutoIt, I am sure we have a way to make it. Can some one here give me some ideas?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

It depends on the size of the input file. But you could do something like this:

  • Read the file to an array using _FileReadToArray
  • Loop through the array and create the output records
  • Write the output records to a new file
Number 2 could be something like:

  • If record[$i] starts with "+" then record[$i+2] or record[$i+2] will start with an "&". If true combine this two records
  • If record[$i] starts with "$" then write an empty record
  • If record[$i] starts with "&" ignore the record (has been processed with the "+" record)
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

quick example from water suggestion

#include <Array.au3>
Global $c = 0,$d = 0
Dim $array[1]
$a = '+wary'& _
@CR&'#adj."here is Chinese translation"'& _
@CR&"&'w??ri"& _
@CR&'$3'& _
@CR&'+welter'& _
@CR&'#n."here is Chinese translation"'& _
@CR&'#vi."here is Chinese translation"'& _
@CR&"&'welt?"& _
@CR&'+whimsy'& _
@CR&'#n."here is Chinese translation"'& _
@CR&"&'hw?mzi:"& _
@CR&'$3'
$b = StringSplit($a,@CR)
_ArrayDisplay($b)
;all upper lines are close or equivalent to _FileReadToArray() func


For $x = 1 To $b[0]
    If StringLeft($b[$x],1)="+" Then
        If $d > 0 Then _ArrayAdd($array,"")
        If $d = 0 Then $d += 1
        Do
            $c+=1
        Until StringLeft($b[$x+$c],1) = "&"
        _ArrayAdd($array,$b[$x]&" "&$b[$x+$c])
        $c = 0
    EndIf
    If StringLeft($b[$x],1)="#" Then _ArrayAdd($array,$b[$x])
Next
$array[0] = UBound($array)-1
_ArrayDisplay($array)
;_FileWriteFromArray() to write it to new file

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites

Try this:

$sText = FileRead("Text.txt")
$aSplit = StringSplit($sText, "$3" & @LF, 2)

Global $i, $j, $k, $s1 = "", $s2 = ""

While $i < UBound($aSplit)
    Switch StringLeft($aSplit[$i], 1)
        Case "+"
            $s1 &= StringStripCR($aSplit[$i]) & " "
            For $k = $i + 1 To UBound($aSplit) - 1
                If StringLeft($aSplit[$k], 1) = "&" Then
                    $s1 &= $aSplit[$k]
                    $i = $k
                    $s1 &= $s2 & @CRLF
                    $s2 = ""
                    ExitLoop
                EndIf
                $s2 &= $aSplit[$k]
            Next
    EndSwitch
    $i += 1
WEnd
MsgBox(0, "", $s1)

Text.txt has the content from your first code box.

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

hi,

Here's something short and sweet if you so prefer :huh2:

;Read and reorder
Local $sNewFileContents=StringRegExpReplace(FileRead("input.txt"),"(\+.+)(\r\n)(#.+\r\n+)(#.+\r\n+)*(&.+)","$1 $5$2$3$4")

;Output to UTF-8 encoded text file
Local $hOutputFile=FileOpen("output.txt",18)
FileWrite($hOutputFile,StringToBinary($sNewFileContents,4))
FileClose($hOutputFile)

;)

-smartee

Share this post


Link to post
Share on other sites

Try this:

$sText = FileRead("Text.txt")
$aSplit = StringSplit($sText, "$3" & @LF, 2)

Global $i, $j, $k, $s1 = "", $s2 = ""

While $i < UBound($aSplit)
    Switch StringLeft($aSplit[$i], 1)
        Case "+"
            $s1 &= StringStripCR($aSplit[$i]) & " "
            For $k = $i + 1 To UBound($aSplit) - 1
                If StringLeft($aSplit[$k], 1) = "&" Then
                    $s1 &= $aSplit[$k]
                    $i = $k
                    $s1 &= $s2 & @CRLF
                    $s2 = ""
                    ExitLoop
                EndIf
                $s2 &= $aSplit[$k]
            Next
    EndSwitch
    $i += 1
WEnd
MsgBox(0, "", $s1)

Text.txt has the content from your first code box.

Br,

UEZ

Hi UEZ, Your code worked just fine. Thank you very much. I combine your code with that of smartee's, and it worked greatly.

Thank you again.

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  
Followers 0