Jump to content

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


 Share

Recommended Posts

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
Link to comment
Share on other sites

@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.

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
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

Link to comment
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

 

Link to comment
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

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

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...