1.

Solve : Replace text lines?

Answer»

Hello, and thanks in advance;

I've been looking for days for some kind of software that could replace text lines from one text source into other text.
Example:

Text 1:
123456,, ABC
123456,, TDA
532456,, JMK

(...)

Text 2:
FIRST text line.
Second text line.
Third line.

(...)

So, with those two text sources, I want to automatically create this one:
123456,, First text line.
123456,, Second text line.
532456,, Third line.

(...)


I know I could just copy/paste every line from text2 to text1, that's what I'm doing now, but the texts are hundreds or thousands lines long.


¿Do you know any software to help with this task?
Thank you for your time and help.Why do not

ABC
TDA
JMK

appear in new file?
Quote from: Salmon Trout on May 18, 2013, 08:14:00 AM

Why do not

ABC
TDA
JMK

appear in new file?

I want them replaced by the text in text 2.
I mean:
"First text line." replaces only "ABC", not the ",," or the text behind it.


Thank you for you fast answer.
Is each line in text1 always 6 characters then 2 commas then 1 space (9 altogether) then text to be replaced?

123456,, ABC

And do text1 and text2 always have the same number of lines as each other?

I'm sorry I have not been enought specific.

Every line in text1 has, in this order:
- A variable number of spaces and characters, INCLUDING alfanumeric and non alfanumeric.
- ",,0,0,0,,"
- And the text I want to replace.


Example of the actual lines I'm working on:

Comment: 0,0:00:00.00,0:00:00.00,Default,,0,0,0,,Only this words must be replaced with line in text2.
Dialogue: 0,0:03:27.77,0:03:28.91,Alfa,,0,0,0,,These are the words in the second line that must be replaced.
Dialogue: 0,0:08:12.62,0:08:13.87,Example,,0,0,0,,And goes on.


I supose the key is a software or script that can automatically replace only the text after the ",,0,0,0,," in text1, with the same line number in text2.


Thank you.
Comment: 0,0:00:00.00,0:00:00.00,Default,,0,0,0,,Only this words must be replaced with line in text2.
Dialogue: 0,0:03:27.77,0:03:28.91,Alfa ,,0,0,0,,These are the words in the second line that must be replaced.
Dialogue: 0,0:08:12.62,0:08:13.87,Example,,0,0,0,,And goes on.
^ ^ ^ ^^ ^^
, , , ,, ,, (7 commas)


So this is the format? (I added spaces after Alfa in the second line to make the format clearer) This looks like CSV format. Do you have Excel?

I ask again:

And do text1 and text2 always have the same number of lines as each other?



Yes, thats the format. I do have excel.
Yes, text1 and text2 have the same number of lines.

A friend recomended me using a macro in Notepad++, I'm INVESTIGATING it.I have hacked out a Visual Basic Script which will do this but I will not finalize it or post it yet because I do not know if you are happy with a command line solution:

File1

Comment: 0,0:00:00.00,0:00:00.00,Default,,0,0,0,,Only this words must be replaced with line in text2.
Dialogue: 0,0:03:27.77,0:03:28.91,Alfa,,0,0,0,,These are the words in the second line that must be replaced.
Dialogue: 0,0:08:12.62,0:08:13.87,Example,,0,0,0,,And goes on.
Dialogue: 0,0:09:27.77,0:09:28.91,Rotate,,0,0,0,,These are the words in the second line that must be replaced.
Dialogue: 0,0:10:12.62,0:10:13.87,Elefante,,0,0,0,,And goes on.
Dialogue: 0,0:12:27.77,0:12:28.91,Catalunya,,0,0,0,,These are the words in the second line that must be replaced.
Dialogue: 0,0:13:12.62,0:13:13.87,Perro,,0,0,0,,And goes on.

File2

Pellentesque nunc augue, mattis vitae rhoncus pharetra, convallis sit amet
lorem. Suspendisse ac arcu erat. Suspendisse euismod mi vel sem porttitor et
interdum nisl auctor. Mauris cursus, neque nec hendrerit faucibus, elit velit
pharetra elit, quis accumsan odio purus eget neque. Aliquam hendrerit tempor
tortor eget ullamcorper. Nulla est leo, elementum vitae aliquam vel, porta
faucibus libero. Sed eget est ut turpis fermentum sagittis. Maecenas
sollicitudin sem a tortor interdum bibendum. Morbi vel purus erat. Suspendisse

Output

Comment: 0,0:00:00.00,0:00:00.00,Default,,0,0,0,,Pellentesque nunc augue, mattis vitae rhoncus pharetra, convallis sit amet
Dialogue: 0,0:03:27.77,0:03:28.91,Alfa,,0,0,0,,lorem. Suspendisse ac arcu erat. Suspendisse euismod mi vel sem porttitor et
Dialogue: 0,0:08:12.62,0:08:13.87,Example,,0,0,0,,interdum nisl auctor. Mauris cursus, neque nec hendrerit faucibus, elit velit
Dialogue: 0,0:09:27.77,0:09:28.91,Rotate,,0,0,0,,pharetra elit, quis accumsan odio purus eget neque. Aliquam hendrerit tempor
Dialogue: 0,0:10:12.62,0:10:13.87,Elefante,,0,0,0,,tortor eget ullamcorper. Nulla est leo, elementum vitae aliquam vel, porta
Dialogue: 0,0:12:27.77,0:12:28.91,Catalunya,,0,0,0,,faucibus libero. Sed eget est ut turpis fermentum sagittis. Maecenas
Dialogue: 0,0:13:12.62,0:13:13.87,Perro,,0,0,0,,sollicitudin sem a tortor interdum bibendum. Morbi vel purus erat. Suspendisse
Thats perfect! Exactly the output that I wanted.
Thank you very much :-).Save as e.g. Process.vbs
USE like this (on command line or you can build a batch file of multiple lines like this)

These file names are just examples to show the syntax. They can be anything you like, just filenames (if they are in the same folder as the script) or full path if you want, and if the path/name has any spaces you must use quotes like here.

cscript.exe //nologo Process.vbs "C:\Files\Input1.txt" "C:\Files\Input2.txt" "D:\Bla Bla\Output New.txt"

Please try this on a COPY file or two (not your main data!) and tell me if it does what you want...

' Must have 3 parameters
'1 input file with first part
' like this
' Dialogue: 0,0:03:27.77,0:03:28.91,Alfa,,0,0,0,,
Ifile1 = Wscript.Arguments(0)
'2 input file with second part to be added
Ifile2 = Wscript.Arguments(1)
'3 output file name
Ofile = Wscript.Arguments(2)

' -----------------------------------------------------------
' Show file information on screen
' delete if not wanted
Wscript.echo "Processing files:"
Wscript.echo "Input file 1: " & Ifile1
Wscript.echo "Input file 2: " & Ifile2
Wscript.echo "Output file : " & Ofile
' -----------------------------------------------------------

Set ObjFSO=Createobject("Scripting.Filesystemobject")
'Open input file 1
Set ObjTS=ObjFSO.Opentextfile("Text1.txt")
'Keep reading lines until all done
Dim arrFileLines1()
i = 0
Do Until ObjTS.Atendofstream
Redim Preserve arrFileLines1(i)
' Read whole line
ThisLine = ObjTS.Readline
'Split at commas
S = Split(ThisLine,",")
'Isolate up to 7th comma & format
NewLine = ""
NewLine = NewLine & S(0) & "," & S(1) & "," & S(2) & "," & S(3) & ","
NewLine = NewLine & S(4) & "," & S(5) & "," & S(6) & "," & S(7) & ",,"
'add to array
arrFileLines1(i) = NewLine
i=i+1
Loop
'Close input file
ObjTS.Close
'Open input file 2
Set ObjTS=ObjFSO.Opentextfile("Text2.txt")
' Read all lines into array
Dim arrFileLines2()
i = 0
Do Until ObjTS.Atendofstream
Redim Preserve arrFileLines2(i)
arrFileLines2(i) = ObjTS.Readline
i=i+1
Loop
'Close input file
ObjTS.Close
' Check same number of lines
' Show error message and quit if not
If ubound(arrFileLines1) <> ubound(arrFileLines2) then
wscript.echo "Error: Files do not have same number of lines"
wscript.echo "file 1" & ubound(arrFileLines1) & "/" & "file 2 " & ubound(arrFileLines2)
wscript.quit
End If

' -----------------------------------------------------------
' Show processed lines on screen
' delete if not wanted
For j = 0 to ubound(arrFileLines1)
wscript.echo arrFileLines1(j) & arrFileLines2(j)
Next
' -----------------------------------------------------------

' Write output file
Set objFile = objFSO.CreateTextFile(Ofile)
for j = 0 to ubound(arrFileLines1)
objfile.WriteLine(arrFileLines1(j) & arrFileLines2(j))
Next
Objfile.Close




Discussion

No Comment Found