Jump to content

How to insert new line after certain characters


Recommended Posts

Greetings,

I am trying to add a new line right after a certain character appears in a .txt document then write certain lines to another file. Here is what I am trying to do in a nutshell:

- Add a new line after every "<" and every ">"

- Insert "PH" at the beginning of certain lines (by counting down after the "newlines" have been entered" as the formatting will always remain the same)

- If I cannot insert "PH" at the beginning of certain lines; delete unneeded lines

- Finally, copy text from RSS.txt file to Weather.txt file

Pertaining to the adding a new line I have been playing around with the following code but I am still trying to figure out how to get a new line after each time "<" and ">" appears in the document.

$file = FileOpen("RSS.txt", 1)

; Check if file opened for writing OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

FileWrite($file, "Line1")
FileWrite($file, "Still Line1" & @CRLF)
FileWrite($file, "Line2")

FileClose($file)

In addition, I have been using the following code to count the lines down and grab the lines I need/want and export them over to a new .txt file

$aRecords[4] = "local weather and other some such information."

; ... finally write file to destinaton
; -------------------------------------
_FileWriteFromArray($DestinFileName, $aRecords, 1)

I am really struggling with trying to pull it all together though. Check out the attached doc's if you have any more questions about my complete code I'm hashing together. If you can provide any assistance that would be wonderful.

I'm searching through the help files as much as I can but can't find specifics yet; I'll keep looking, too.

Cheers~

RSS.txt

add lines to RSS-txt file.au3

Link to comment
Share on other sites

  • Moderators

Your request for help isn't very helpful in describing what you're really trying to do unfortunately.

You mention "new line", am I to assume that's a carriage return and a line feed ( CRLF ) or that you want to actually insert new "text" literally after the "<" and after the ">"?

You mention putting "PH" on "certain" lines, well, what "certain" lines would those be?

Some of the first things I generally do when I'm going to manipulate a text file:

Get as much comparison information ( different txt files that I'd be dealing with ), so I can compare to see what issues I may run into.

Setup "rules" (I want to know exactly how I'm going to proceed).

Choose my string functions I'll be utilizing, will it be StringRegExp, StringRegExpReplace(), StringReplace(), StringSplit(), etc.

Based on that, setup simple test functions to see if my patterns or ideas are on track and easily achievable.

----

At first glance, I saw your request as simple as

FileWrite($outfile, StringReplace(FileRead($file), "<", "Text or CRLF Here"))
but then the more I read, the more I got confused.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I'm trying to take the HTML text in that RSS.txt file and cut out all of the garbage. If there is a way I can drop all of the <>'s and such I can cut the fat and keep the information I need only. If I were to add /n or new lines (or a return after each < or > then I could begin separating the text).

Then hopefully I can begin to separate it so it looks like this:

Last Updated: Jan 22 2011, 11:51 pm MST

Sat, 22 Jan 2011 23:51:00 -0700

Weather: Partly Cloudy

Temperature: 55.0 °F (12.8 °C)

Dewpoint: 28.9 °F (-1.7 °C)

Relative Humidity: 37 %

Wind: Calm

Visibility: 10.00 miles

MSL Pressure: 1010.5 mb

Altimeter: 29.87 in Hg

This way I can pull it into another program in a little more formatted way.

Also I tried:

FileWrite($outfile, StringReplace(FileRead($file), "<", "Text or CRLF Here"))

and it didn't seem to insert anything into the file? Maybe I didn't something incorrect.

If I could delete anything to the right of a "<" and stop deleting at the closing brace ">" (including both of the "<" ">"'s along with it) that would be great. This would leave just the text and from there I can figure it out....I think

Edited by gr8BigGeek
Link to comment
Share on other sites

If I've understood what you are trying to do then this should clean up the data as you require.

#include <array.au3>
Global $aArray
Global $sData = FileRead("C:\rss.txt")                                                   ; Read the RSS File you will need to change the path to match the location on your PC
$sData = StringRegExpReplace($sData,"\n( |\t)+",@LF)                    ; Remove whitespace at the start of lines
$sData = StringRegExpReplace($sData,"(?s)<[^>]*?>",@CRLF)    ; Remove < and  > and all the text in between
$sData = StringRegExpReplace($sData,"(\r\n)+",@CRLF)                ; Remove blank lines in data
$sData = StringRegExpReplace($sData,"^(\r\n)+|(\r\n)+$","")            ; Remove blank lines at start or end of data
$aArray = StringSplit($sData,@CRLF,3)                                                ; Convert data to an array
_ArrayDisplay($aArray,"Cleaned Data")                                                 ; Display array so you can see the cleaned data.

BTW The RSS file contains XML data not HTML

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

Bowmore,

Awesome and thank you for the reply! I figured out how to do it with vbscript (.vbs) and batch (.bat) formats. I currently have 3 .bat's and 1 .vbs working together in a series to accomplish this. I'm very grateful for your response and am very excited to try your code. It's not very easy to write (especially if you are trying regular expressions) at least for me anyway.

I am just about to head out the door right now but I have copied/pasted your code and will try it this evening. I will be sure to get back with you about this code though.

Thank you again for all of your time you have put into this.

Your geek~

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