Solved insert a carriage return into a txt file

Hi all.I believe I have a challenge for you all today.Basically I just need to insert a carriage return into a txt file before a certain (known ) string using a .bat file. Basically the txt file has about 2000 records clumped together. The findstr would be perfect as I am looking for the reference number of the user, but the findstr just returns the entire file as there are no carriage returns. I need to insert a carriage return before every instance of the word “header” and after every instance of the word “MTR”.If I can get the carriage return in those places it would be great because the findstr will return every bit of the record between the “Header” and “MTR”.However as easy as this may sound there are a few catches.1. there are currently no carriage returns in the file.2.The file is about 70mb3.Although there are delimiters in the file, the actual number of delimiters changes per record and is always well above the 26 allowed by tokens.4.there are a huge number of lines (over 2000 a4 pages) worth of records, using a loop would be inpractable ?5.Findstr just returns the entire txt file because there are no carriage returns6.There are about 2000 instances of the word “header”

Sample formatHeader’there is just so’much text that : goes here and the ‘amount of dilimiters’? is just socrazy’the RECORD NUMBER MAYBE BOUT HERE+and then there would be more stuff.This∧ the dilimiters would:go on a further+50lines’before it hits the’MTR’Header:and were off again for another record of 50 lines’MTR The final appearance (if possible to export into a .txt file with changes ?) it to look like the below as the find str will grab everything between the “header” and “MTR”.the RECORD NUMBER will be based on user input but I can do that.Here is what im hoping for

Header’there is just so’much text that : goes here and the ‘amount of dilimiters’? is just socrazy’the RECORD NUMBER MAYBE BOUT HERE+and then there would be more stuff.This∧ the dilimiters would:go on a further+50lines’before it hits the’MTR’

Header’there is just so’much text that : goes here and the ‘amount of dilimiters’? is just socrazy’the RECORD NUMBER MAYBE BOUT HERE+and then there would be more stuff.This∧ the dilimiters would:go on a further+50lines’before it hits the’MTR’

Header’there is just so’much text that : goes here and the ‘amount of dilimiters’? is just socrazy’the RECORD NUMBER MAYBE BOUT HERE+and then there would be more stuff.This∧ the dilimiters would:go on a further+50lines’before it hits the’MTR’

Try looking at the file and see if the dump is using a strange character for line breaks. If it's only using "\n" or "\r" instead of "\n\r", then it's an easy fix. Also, if each record is guaranteed to be X characters long, it's also easy to fix.

Hi Razor.The problem is that there are no line breaks at all.I must have been thinking wrong when I said carriage returns, I meant line breaks.Basicly the file is one big huge 2000 page single line :(.The record will be different each time, but will always have "header" at the start of the record and "MTR" at the end.I was thinking of find and replace ie find :"Header" and replace with header \n ?, that type thing ?

actually just re-read that and confused myself. Basicly it's one long dump with no line breaks or anything.I need to find the word "Header" and insert a line break before every instance of this word. Hope this helps ?

i think you're sunk if you're restricted to native batch. vbscript (native to windows) could handle it with facility, using stdin.read method to get "bites" of the file, then test for each "frame" if matches your specs. Let me know if interested. (I don't want to work on it only to have "it must be native BATCH script ONLY").

Hi nbrane . Could it be VB that word use's as there are loads and loads of restrictions on the computer, or even an Icon I can just run ?,I'm not sure if I can run a VB macro outside of word thou. What kind of way would you think is best ?

DOS probably won't handle the linebreak though, so you need to save that as a file & run it in sed with -f

It's a single command to do that w/ sed (built into a any linux machine if you can get access to one)

Or like the other poster said, you could use VB / .NET to do this, but I would use something like the split() command in perl to split the string into an array by your key & then just loop through the array & write it out with line breaks.

Hi fmwap.I am limited to whats built into windows sadly enough :(. Really all I need is a bit of code to insert a line break after each instance of the word "Header". I can do this in MSword using find and replace and the "^l" but I would just rather an automated way to do it, as it will have to be done every day as new records are added daily.I do have a macro in MSword for this, but not sure how to use the macro outside of MSword. Using a batch would be the holy grail, but beggers can't be chosers :(

Actually I havent been clear of this. Everyday I use a .bat to group all the data files (.txt files) together into one big .txt file which makes it easy to find what im looking for.

However what im trying to do now is simply filter the info.So I enter the record number into a .bat file. The .bat file finds the record number and returns everything between the "header" and "mtr" of that particular record, and then keeps looking incase there are more instances or amendments to the record. The problem is because there are no line breaks the findstr is returning the whole document rather than just the the required record. Not sure if this helps ?

Everyday I use a .bat to group all the data files (.txt files) together into one big .txt file which makes it easy to find what im looking for. How are you doing that, and just how many records are in each file?

EDIT: To clear up what is/isn't native to Windows, you have three scripting languages that install with WinXP. They are batch scripts, VBScripts, and JScripts. If you have Win7, you also have POWERSHELL!, but you don't, so there you go.

Hi Boys. I can't download anything onto the computer as they are restricted access :(. I use the copy file1.txt+ffile2.txt+file3.txt > master.txt

This appends all the files into one as the record could be in any file so instead of searching 3 files.(Actually there is a new file created everyday so it appends them into the one big one. I now have them in one file. The would be about 500 records in each file every day. I can't use the powershell as im not allowed download anything. Can I use a VBmacro outside of word for a word file. I ie, I can export the master.txt file to a .doc file instead and then run a VB macro against the .doc file ?

You're confusing Visual Basic for Applications and Visual Basic Script. they're separate languages with similar names. And yes, you could run a VBScript on a text file. You can also run a VBA macro on a text file, but that requires Word (or some other MS Office program) to be open.

Well at this stage I will take any help or sugestions or code that can in anyway help.Floor is open lads, no wrong answers. Can you advise how I could run a VBScript on a text file ?, I.e do I make it in note pad and save as a .vbf or what ever extension.Any ideas at all will help. I don't need the search for record function, I just need the line break code.Maybe I can even call the VBscript from a .bat so it's still a one click for the user ?

Could you create a text file with only a cr/lf in it, and include it in your copy command before all the data files?

copy file1.txt+crlf.txt+file2.txt+crlf.txt+file3.txt > master.txt

I'm actually surprised the copy file+... doesn't inject a cr/lf after each file anyway, making the contents of the files a record on a new line of master.txt. I was experimenting with both copy and type (looping through txt files), and came up with nearly identical "master" files:

Ok a few answers to your questionsRazor2.3 :1) The files come from different sources, and they just send them as a dump.I tried to get them to insert the line break but no go.

Orangeboy2) copy file1.txt+crlf.txt+file2.txt+crlf.txt+file3.txt > master.txtwont work because I need the line breaks in the file 1.txt not between the files in the master.txt, i.e no need for gaps between the insertion of files, I need the gaps before a recurrance of the string "header".The is a occurance of the word header about 200 times per file, so its not just one per file .I mearly merge the files as it makes it easier to search.

3)I think the above code is inserting line breaks to break up the actual files rather than the contents. What I actually need is line breaks in the actuall contents of each file.

So lets just assume I have one file called master.txt and need to insert a line break after every occurance of the word Header(about 2000 occurances of word header). The fact that the files are merged dosn't really matter as the end result is still one big file.

I really appracaite all this help, I honestly do, but Im not sure if you guys understand what im trying to do. It's like the file master.txt has may occurances of the word header. 1.I need to insert a line break before every occurance of the word "header" in the master.txt file

2.Any code that is built into windows XP is cool, what ever you think is the best,but I can't download anything :(.

3.This code will be run every dayso thats why a .bat would be cool, but we can manually click on it (i.e it shoulden't auto run at a certan time every day, we can run t ourselfs.

Okay, final question: Can we see the first 10 records or so? There seems to be some inconsistency in your example. If you don't mind showing me, but don't want to show everyone, you can PM me the data, or PM me asking for an email address.

Hey daragh_c,I understand that you need the individual records separated. With the "file" records broken down into rows in the master.txt, wouldn't it now be possible to start using some of the more traditional methods of parsing? In other words, instead of trying to tackle one giant record without breaks, you now have at least a little more manageable records to deal with, and may not be constrained to some of the limitations you found earlier (line length with FINDSTR, number of tokens with FOR, etc...)

orange boy. The file1.txt is HUGE, there are over 200 records each with a header and an MTR per file !! A new file is created everyday and must be added to the previous file. The findstr works perfect once I have the file seperated (as above the way I want it).I simulated this by opening the master.txt in word using find and replace to make the space.I just need an automated way of doing this find and replace.Also the actuall files are well above the token and dilim lenght.The findstr will word, but will return the entire file because of the lack of line breaks :(

Razor, I had a look at the MTR as well and tryed adding a line break after every occurance, but figured it was the same code.Also in the actual file there are about 80 " ' "s in it so it runs out of tokens.Also the mtr is never after x amount of charaters, but it is the last thing on each record.

EDIT: You know what? This has been dragging on too long as is. You can probably figure out how to fix the search string yourself. Just save this as a VBS file, then drag your text file over the VBS file:

Thats amazing, it really is, but, like a twat that I am, i left out a thing I forgot to add in. the MTR is actuall 'mtr= XX' where XX' is a number. This number is vital. My bad I know.

Can i just change the Const magicStr = "'MTR'" to Const magicStr = "'MTR'"+??" ' " or the next two digits. I think the MTR= will only ever be a double didgit figure. Really sorry and that VB script works cool. I remember now why I was hoping to get the line break before the "HEADER" as it would have avoided this.

I also ran it from the cmd by typing "code.vbs text.txt" and it exported a new file - which is super amazing !!!!

fair point Razor,fair point. It wil be always 'header=X' where X is a number,but it could be 'header=XXX. However, if one one these records needs to be reloaded we will always manually change this to 1 so in short the XXX dose not need to be presereverd.

Also I note that the first instance of the MTR did not put a line break, but it's ok, one record is fine.I don't think im holding anything else back... for now :)

Just one last question, and yes I know I've used considerable resources.., Basicly All I want now is when I type the record number the findstr is returning the entire record (Which is perfect). However, is there any way that when I find the record number the batch script will then search back up wards for a line of text which could be X amount of lines above. For instance the layout is like this

hello An easy wayBring your file to easy path.Go to command [dos] mode.Change directories and locate file.Use "edit" command.EDIT xxx.txtIn 'edit' , use 'search' . Use 'replace'.In 'find', put characters that you have at end of each line.In replace give <ctrl+p><ctrl+j><ctrl+p><ctrl+m> .[this is ^p to input special character then ^j for line feed ,same ^p^m for carriage return]It will ask and do replacements. Some funny character will show. Try ; Then do it replace-all.Note- DOS can handle only upto 64k size.So may need splitting................it works, all the best , pl revert to me at ablewasi12@gmail.com , if query / successedit help at http://www.computerhope.com/edithlp...

The information on Computing.Net is the opinions of its users. Such
opinions may not be accurate and they are to be used at your own risk.
Computing.Net cannot verify the validity of the statements made on this
site. Computing.Net and Purch hereby disclaim all responsibility
and liability for the content of Computing.Net and its accuracy.