Putting line of txt in correct place in .txt

July 10, 2010 at 11:58:04
Specs: Windows XP
Hi Guys.

I have a text file that looks like the following

Important line XXX
Important line ZZZ
Important line YYY

Record -------------------------------------------------------------------------- END of record
Record -------------------------------------------------------------------------- END of record
Record -------------------------------------------------------------------------- END of record

What I need it to look like is this

Important line XXX
Record -------------------------------------------------------------------------- END of record

Important line ZZZ
Record -------------------------------------------------------------------------- END of record

Important line YYY
Record -------------------------------------------------------------------------- END of record


So basically the correct “important line “has to be inserted into the line just above the “record”

The first “important line” will always be for the first record
The second “important line” will always be for the second record
The third “important line” will always be for the first record and so on .....


There are no line breaks at all between the record and the  END of record,
so I was hoping to be able to maybe collect all the “important line” into an array and then find the next line break and insert the next count in the array so it should be inserting the next “important line” after each line break .

If there is an easier way please let me know,. The only trick is that this has to be a .vbs or .bat approach if that’s ok ?
Many thanks in advance


See More: Putting line of txt in correct place in .txt

Report •


#1
July 11, 2010 at 06:42:27
If I'm reading this correctly, you have one
text file with two types of data: important
lines
and records. The important
lines are at the top and are separated from
the records (which occupy one line each) by
one blank line.

Try the following [just prints result to screen]:

@echo off

setlocal EnableDelayedExpansion

set n=0
set nn=0
set ArrayDone=false
for /f "tokens=1* delims=[]" %%a in ('type somefile.txt ^| find /n /v ""') do (
  if "!ArrayDone!" equ "true" (
    set /a nn+=1
    for %%i in (!nn!) do echo.!arr%%i!
    echo.%%b
  ) else (
    if "%%b" neq "" (
      set /a n+=1
      set arr!n!=%%b
    ) else (
      set ArrayDone=true
    )
  )
)
for %%i in (1, 1, %n%) do set arr%%i=


BTW, don't give up if it doesn't appear to work
with your actual data - there's probably ways
around it...



Report •

#2
July 11, 2010 at 09:04:16
Hi Mark. The records actually about 25 - 75 lines of text, although there are no actual line breaks in the records. The "Importand Info are just one line long.They are seperated by a line break.I can send you an example of the info if you want ?

Daragh


Report •

#3
July 11, 2010 at 09:44:50
...The records actually about 25 - 75 lines of text, although
there are no actual line breaks in the records...

You should check out Notepad++. I do not believe that it
wraps text after x number of characters like notepad.exe
does. At least I have not reached Notepad++'s limit yet. You
would then be able to see each (line-breaked) record as it is,
and not how some other text editors present it...

http://notepad-plus-plus.org/

Insomniac at large


Report •

Related Solutions

#4
July 11, 2010 at 12:56:01
daragh_c, please do as orangeboy recommends and
download Notepad++. Once downloaded and installed,
open your file, make sure wordwrap is off, put the cursor
at the end of one of the longest record lines and make
note of the number after 'Col :' in the status bar then
post here for inspection. It could be that the lines are
actually too long for a batch solution in which case you
may have to repost for vbs solution (which I'm not
overly familiar with...).


Report •

#5
July 11, 2010 at 14:49:11
Thanks lads, I'll repost this under the VBS, as I am not allowed download software onto computer. Thanks a mill anyways.

D


Report •

#6
July 12, 2010 at 15:22:19
For a start, I hope there's a typo in here:

"The first “important line” will always be for the first record
The second “important line” will always be for the second record
The third “important line” will always be for the first record and so on ....."

[2] Also, this bit:

"The records actually about 25 - 75 lines of text, although there are no actual line breaks in the records."

There either are line breaks or there aren't. Not much grey area there.

[3] If this is going to get done you'll probably need to supply a meaningful chunk of the file.


=====================================
Helping others achieve escape felicity

M2


Report •

#7
July 13, 2010 at 13:34:45
here's a vbs that might work... not tested with long records:
dim fso
set fso=createobject("scripting.filesystemobject")
set t1=fso.opentextfile("test",1)
'first, skip an initial blank line
x=t1.readline
if ltrim(x)="" then x="yes"
do while not t1.atendofstream and x<>""
x=t1.readline
loop
set t2=fso.opentextfile("test",1)
set t3=fso.createtextfile("new")
do while not t1.atendofstream
y=t2.readline
t3.writeline(y)
for i=1 to 2
x=t1.readline
t3.writeline(x)
next
loop
t1.close
t2.close
t3.close

Report •

#8
July 14, 2010 at 05:28:50
Hi Nbrane.
The above works perfect.Sadly there is just one issue. The way the text is formated previous to arriving into the .txt file is that there is always a blank line at the start of the text file. This is screwing up your script. I have manually deleted the line break on the first line and your script is bang on. However I was wondering if you could incoroperate into your above script to tell the script to either delete the first occurance of a line break or simply ignore the first line of the .txt file. If I can (means you of course) can get around this, then the script will work perfect !.

Let me know if I can help in any way ?.
Many thanks.

Ps, I know you have done loads so I posted this problem on another post incase you felt this is to much of an ask, which would be 100% fair.


Report •

#9
July 14, 2010 at 06:03:13
Actually belay that, I got it with

contents = Replace(contents,VbNewLine,"",1,1)


Report •

#10
July 14, 2010 at 12:59:53
yes, that's no problem. kind of hacky way to do it, but just add one line, at this snippet (near the top):
'add the following line, the rest is just reiterated snippet
x=t1.readline
x="yes"
do while not t1.atendofstream and x<>""
x=t1.readline
loop
'-------- end snippet
i'll fix it in my response above as well. good luck.

Report •

#11
July 15, 2010 at 01:13:31
see below

Report •

#12
July 15, 2010 at 02:47:37
Hi Nbrane.
I have made a huge human error while demonstrating this code.
The format I originally showed you was incorrect (but only a little)
There ARE actual line breaks between the records. (Note there are no line breaks IN the record, just linebreaks after each record.I am not sure if this will require pages of code or a simple tweak ?
The format as it stands before it goes through your .vbs script is as below
For instance :
<<BLANK LINE>>
Important line XXX
Important line ZZZ
Important line YYY
<<BLANK LINE>>
Record -------------------------------------------------------------------------- END of record
<<BLANK LINE>>
Record -------------------------------------------------------------------------- END of record
<<BLANK LINE>>
Record -------------------------------------------------------------------------- END of record
After it comes out of your code it actually looks like ;

<<BLANK LINE>>
Record -------------------------------------------------------------------------- END of record
Important line XXX
<<BLANK LINE>>
Important line ZZZ
Record -------------------------------------------------------------------------- END of record
Important line YYY
<<BLANK LINE>>
Record -------------------------------------------------------------------------- END of record

The “important lines” are not where they are supposed to be but this is totally my fault for been silly and not displaying the original output as it is. I’m wondering where you have the code :
y=t2.readline
t3.writeline(y)
x=t1.readline
t3.writeline(x)
loop
If there was another variable to account for the extra line breaks so maybe it would look like ;

y=t2.readline
y2= writeline(y)
blankLine=t1.readline
t3.writeline(blankLine)
x=t1.readline
t3.writeline(x)
loop
or something like that ?. It’s just that it’s all soooooo close, and yet so far …

Just to recap I’m looking for
Important line XXX
Record -------------------------------------------------------------------------- END of record
<<BLANK LINE>>

Important line ZZZ
Record -------------------------------------------------------------------------- END of record
<<BLANK LINE>>
Important line YYY
Record -------------------------------------------------------------------------- END of record

If I can do anything to help just let me know.
Many thanks,
d


Report •

#13
July 15, 2010 at 11:46:18
(edited to fix a mistake...)
mebbe try it with this mod:
set t3=fso.createtextfile("new")
do while not t1.atendofstream
y=t2.readline
t3.writeline(y)
for i=1 to 2
x=t1.readline
t3.writeline(x)
next
loop
'------- end snip
hopefully the dbl-readline should pick up the extra blank

Report •


Ask Question