asiawatcher

How on earth do we add a string on EVERY OTHER line of a text file ?

15 posts in this topic

#1 ·  Posted (edited)

as title says.. i dont want to add text to beginning or end of file

 

i want to open a text file and pre-pend something on the beginning of EVERY other LINE (the ones without #'s and ignore also the first 4 lines)

 

check this out

 

#EXTM3U
#EXT-X-TARGETDURATION:10
#EXT-X-MEDIA-SEQUENCE:5448549
#EXTINF:12.0
C8B6E0C508FC74915BAA94D116F142165-frag5448549.ts?token=822259&gid=C8B6E0C508FC74915BAA94D116F142165&range=12006834126320-12006835068012
#EXTINF:12.0
C8B6E0C508FC74915BAA94D116F142165-frag5448550.ts?token=822259&gid=C8B6E0C508FC74915BAA94D116F142165&range=12006835068012-12006836991064
#EXTINF:12.0
C8B6E0C508FC74915BAA94D116F142165-frag5448551.ts?token=822259&gid=C8B6E0C508FC74915BAA94D116F142165&range=12006836991064-12006838592636
#EXTINF:12.0
C8B6E0C508FC74915BAA94D116F142165-frag5448552.ts?token=822259&gid=C8B6E0C508FC74915BAA94D116F142165&range=12006838592636-12006839630396
#EXTINF:12.0
C8B6E0C508FC74915BAA94D116F142165-frag5448553.ts?token=822259&gid=C8B6E0C508FC74915BAA94D116F142165&range=12006839630396-12006840960308
#EXTINF:12.0
C8B6E0C508FC74915BAA94D116F142165-frag5448554.ts?token=822259&gid=C8B6E0C508FC74915BAA94D116F142165&range=12006840960308-12006842139632
#EXTINF:12.0
C8B6E0C508FC74915BAA94D116F142165-frag5448555.ts?token=822259&gid=C8B6E0C508FC74915BAA94D116F142165&range=12006842139632-12006843484584
#EXTINF:12.0
C8B6E0C508FC74915BAA94D116F142165-frag5448556.ts?token=822259&gid=C8B6E0C508FC74915BAA94D116F142165&range=12006843484584-12006845024868
#EXTINF:12.0
C8B6E0C508FC74915BAA94D116F142165-frag5448557.ts?token=822259&gid=C8B6E0C508FC74915BAA94D116F142165&range=12006845024868-12006847775684
#EXTINF:12.0
C8B6E0C508FC74915BAA94D116F142165-frag5448558.ts?token=822259&gid=C8B6E0C508FC74915BAA94D116F142165&range=12006847775684-12006850449984

i want to add an IP to the beggining of every other line (except the first 4 lines) and resave the file so it will be like this

FROM THIS

#EXTM3U
#EXT-X-TARGETDURATION:10
#EXT-X-MEDIA-SEQUENCE:5448549
#EXTINF:12.0
C8B6E0C508FC74915BAA94D116F142165-frag5448549.ts?token=822259&gid=C8B6E0C508FC74915BAA94D116F142165&range=12006834126320-12006835068012

........

TO THIS (NOTE THE http://192.168.100.1:1111/ ADDED)

#EXTM3U
#EXT-X-TARGETDURATION:10
#EXT-X-MEDIA-SEQUENCE:5448549
#EXTINF:12.0
http://192.168.100.1:1111/C8B6E0C508FC74915BAA94D116F142165-frag5448549.ts?token=822259&gid=C8B6E0C508FC74915BAA94D116F142165&range=12006834126320-12006835068012

........

 

how can we do this is it possible ? I cant seem to find anything about this

 

cheers

Edited by asiawatcher

Share this post


Link to post
Share on other sites



So, what have you got so far?:)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

asiawatcher,

You want to prepend all lines not starting with an "#"?

kylomas

Edited by kylomas

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

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

yes exactly kylomas

 

cheers

Edited by asiawatcher

Share this post


Link to post
Share on other sites

kickstarter i got nothing so far couldn't find an example that can get me going, all i could find was to add things in the start and end of file

 

cheers

Share this post


Link to post
Share on other sites

@asiawatcher,

Is your text file always having this text "#EXTINF:12.0" then insert your IP add after it?

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Use FileReadToArray. Loop through the array using For . . . To  . . .  Step 2 . . . Next, and concatenate the strings using the & operator.

Edited by czardas

Share this post


Link to post
Share on other sites

Kickstarter yes file always same and same number of fields only some variables change thanks

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

@asiawatcher,

You can play with this.:D

#include <file.au3>
$sFilePath = @ScriptDir & "\IP.ini"
Local $hFileOpen = FileOpen($sFilePath)

If $hFileOpen = -1 Then
   MsgBox($MB_SYSTEMMODAL, "", "An error occurred opening file.")
   Exit
EndIf

$string = _ReplaceStringInFile (@ScriptDir & "\IP.ini", "#EXTINF:12.0", "#EXTINF:12.0"&@CRLF&"http://"&@IPAddress1&"/")
StringReplace($string, @CRLF, "")
FileClose($hFileOpen)

 

Edited by KickStarter15
Ooops! missed something.
1 person likes this

Share this post


Link to post
Share on other sites

Amazing but this produces records like this

#EXTINF:12.0
http://192.168.100.110:11111/
C8B6E0C508FC74915BAA94D116F142165-frag5450389.ts?token=66095835&gid=C8B6E0C508FC74915BAA94D116F142165&range=12010595180812-12010596423868

instead of

 

#EXTINF:12.0
http://192.168.100.110:11111/C8B6E0C508FC74915BAA94D116F142165-frag5450389.ts?token=66095835&gid=C8B6E0C508FC74915BAA94D116F142165&range=12010595180812-12010596423868

 

how to remove the new line ?

 

cheers

Share this post


Link to post
Share on other sites

tried all of the below wont work

 

StringReplace($string, @CRLF, "")

StringReplace($string, @CR, "")

StringReplace($string, @LF, "")

StringStripCR($string)

 

Share this post


Link to post
Share on other sites

 Using _ReplaceStringInFile()  as did KickStarter15 in post #9, there is this example.

#include <file.au3>

; ----Create file ------------
Local $sFilePath = @ScriptDir & "\IP.txt"
Local $hFileOpen = FileOpen($sFilePath, 10) ;  $FO_OVERWRITE (2) = Write mode (erase previous contents) + $FO_CREATEPATH (8) = Create directory structure if it doesn't exist.
FileWrite($hFileOpen, StringRegExpReplace(FileRead(@ScriptFullPath), "(?s)^.*#cs\v+|\v+#ce.*$", "")) ; Get block comment from this script and write to file.
FileClose($hFileOpen)

ConsoleWrite(" -------- before  -------------" & @CRLF & FileRead($sFilePath) & @CRLF)
_ReplaceStringInFile($sFilePath,@CR, "") ; All Carriage Return + Line Feeds will become Line Feeds only.
; If you know for certain that only @CRLF are used in the file then you can eliminate the previous _ReplaceStringInFile() and in the next
; _ReplaceStringInFile() change the two @LF's to @CRLF's.
_ReplaceStringInFile($sFilePath, "#EXTINF:12.0" & @LF, "#EXTINF:12.0" & @LF & "http://" & @IPAddress1 & "/")

ConsoleWrite(" -------- after --------------" & @CRLF & FileRead($sFilePath) & @CRLF)
FileDelete($sFilePath)

#cs
#EXTM3U
#EXT-X-TARGETDURATION:10
#EXT-X-MEDIA-SEQUENCE:5448549
#EXTINF:12.0
C8B6E0C508FC74915BAA94D116F142165-frag5448549.ts?token=822259&gid=C8B6E0C508FC74915BAA94D116F142165&range=12006834126320-12006835068012
#EXTINF:12.0
C8B6E0C508FC74915BAA94D116F142165-frag5448550.ts?token=822259&gid=C8B6E0C508FC74915BAA94D116F142165&range=12006835068012-12006836991064
#EXTINF:12.0
C8B6E0C508FC74915BAA94D116F142165-frag5448551.ts?token=822259&gid=C8B6E0C508FC74915BAA94D116F142165&range=12006836991064-12006838592636
#EXTINF:12.0
C8B6E0C508FC74915BAA94D116F142165-frag5448552.ts?token=822259&gid=C8B6E0C508FC74915BAA94D116F142165&range=12006838592636-12006839630396
#EXTINF:12.0
C8B6E0C508FC74915BAA94D116F142165-frag5448553.ts?token=822259&gid=C8B6E0C508FC74915BAA94D116F142165&range=12006839630396-12006840960308
#EXTINF:12.0
C8B6E0C508FC74915BAA94D116F142165-frag5448554.ts?token=822259&gid=C8B6E0C508FC74915BAA94D116F142165&range=12006840960308-12006842139632
#EXTINF:12.0
C8B6E0C508FC74915BAA94D116F142165-frag5448555.ts?token=822259&gid=C8B6E0C508FC74915BAA94D116F142165&range=12006842139632-12006843484584
#EXTINF:12.0
C8B6E0C508FC74915BAA94D116F142165-frag5448556.ts?token=822259&gid=C8B6E0C508FC74915BAA94D116F142165&range=12006843484584-12006845024868
#EXTINF:12.0
C8B6E0C508FC74915BAA94D116F142165-frag5448557.ts?token=822259&gid=C8B6E0C508FC74915BAA94D116F142165&range=12006845024868-12006847775684
#EXTINF:12.0
C8B6E0C508FC74915BAA94D116F142165-frag5448558.ts?token=822259&gid=C8B6E0C508FC74915BAA94D116F142165&range=12006847775684-12006850449984
#ce

 

1 person likes this

Share this post


Link to post
Share on other sites

excellent cheers

Share this post


Link to post
Share on other sites

@asiawatcher,

Apology if I left you hanging with the code above. I was on leave yesterday.:sweating:

Anyways, if this has been fixed already, then it's good. But if not then you can use this:

#include <String.au3>
#include <file.au3>
$sFilePath = @ScriptDir & "\IP.ini"
Local $hFileOpen = FileOpen($sFilePath)

If $hFileOpen = -1 Then
   MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")
   Exit
EndIf
_ReplaceStringInFile ($sFilePath, "#EXTINF:12.0", "#EXTINF:12.0"&@CRLF&"http://"&@IPAddress1&"/")
_ReplaceStringInFile ($sFilePath, "#EXTINF:12.0"&@CRLF&"http://"&@IPAddress1&"/"&@CRLF, "#EXTINF:12.0"&@CRLF&"http://"&@IPAddress1&"/")
FileClose($hFileOpen)

As what Malkey said in post 12.:)

 

KS15

Share this post


Link to post
Share on other sites
On 25/9/2017 at 5:56 AM, kylomas said:

You want to prepend all lines not starting with an "#"?

 

On 25/9/2017 at 6:35 AM, asiawatcher said:

yes exactly kylomas

So literally, it means this  :)

$txt = FileRead("test.txt")
$ip = "http://192.168.100.110:11111/"
$new = StringRegExpReplace($txt, '(?m)(?=^[^#])', $ip)
Msgbox(0,"", $new)

 

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