I have a loop that finds duplicate lines in a .ini file. I can happily find the duplicate lines, and write new lines to the file using FileSystemObject, however... I can't seem to find out how to delete the duplicate lines. What I want to do is delete the lines by line number as I have identified the relevant line number already.
Is there a native way to do this, or is it a case of rewriting the file minus the duplicate lines?
Any help is greatly appreciated.
Thanks.
My method of finding the duplicate entry is as follows:
Do While Not file.AtEndOfStream
intLineNumber = intLineNumber + 1
strReadLineText = file.ReadLine
If strSearchText <> "" And InStr(strReadLineText, strSearchText) > 0 Then
session("message") = "Line Exists on " + Cstr(intLineNumber)
'' # delete duplicate line...
End If
Loop
file.Close()
You can see where my comment is, is where I wish to remove the line that is found.
There are no built-in methods to do this in VBScript (or the intrinsic objects).
My vote would be to use string concatenation to accomplish this; i.e. instead of deleting lines, create a new file, line by line, and only append to the string if the line doesn't exist in the string you're building.
Related
I am new to Julia so sorry if this question is obvious.
I am trying to use Julia to help me run a series of finite element models, which use a text input file to give instructions to the finite element solver. Basically, I would like to use Julia to read in the base input file, edit some parameters on some lines of the file and then write it as a new file. I am getting hung up on a couple things though.
Currently, I am reading in the file like this
mdl = "fullmodelSVTV"; #name of input file
A = readlines(mdl*".inp")
This read each line from the file in as a separate string in a vector which I like because it makes it easier to edit the sections I want but it also makes things more difficult when I try to write to a new file.
I am writing the file like this.
io = open("name.inp","w")
print(io,A)
close(io)
When I try to write to a new file the output ends up look like this
Output from code
which is ["string at index 1","string at index 2","string at index 3"...].
What I would like to do is output this the exact same way is it is read in with string at each index of the vector on its own line. I would also like to remove the brackets and quotation marks from the file, as they might interfere with the finite element solver.
I think I have found a way to concatenate all of the strings at each index and separated them with a new line like shown below.
for i in 1:length(A)
conc = conc*"\n"*lines[i]
end
The issue with this is that it takes a long time to do given the size of the input files I am working with and I feel like there has to achieve my goal.
I also cannot find a way to remove the brackets or quotation marks when writing the file.
So, I'm wondering if anyone has any advice for a better way to write these text files in terms of both concatenating all of the strings from the vector when outputting as well as outputting without the brackets and quotation marks.
Thanks, any advice is appreciated.
The issue with print(io,A) is that it is printing a representation of the vector, but in fact you want to print each element of the vector. To do so, you can simply print each line in a loop:
open("name.inp", "w") do io
for line in A
println(io, line)
end
end
This avoids the overhead of string concatenation.
I have an application where I create a .csv file and then create a .xlsx file from the .csv file. The problem I am having now is that the end of rows in the .csv have a trailing space. My database is in MarkLogic and I am using a custom REST endpoint to create this. The code where I create the .csv file is:
document{($header-row,for $each in $data-rows return fn:concat('
',$each))}
I pass in the header row then pass each data row with a carriage return and a line feed at the beginning. I do this to put the cr/lf at the end of the header and then each of the other lines except for the last one. I know my data rows do not have the space at the end. I have tried to normalize the space around $each and the extra space is still present. It seems to be tied to the line feed. Any suggestions on getting rid of that space?
Example of current output:
Name,Email,Phone
Bill,bill#company.com,999-999-9999
Steve,steve#company.com,999-999-9999
Bob,bob#company.com,999-999-9999
. . .
You are creating a document that is text() node from a sequence of strings. When those are combined to create a single text(), they will be separated by a space. For instance, this:
text{("a","b","c")}
will produce this:
"a b c"
Change your code to use string-join() to join your $header-row and sequence of $data-rows string values with the
separator:
document{ string-join(($header-row, $data-rows), "
") }
After spending a few hours trying to figure this out (From questions already asked and other places), I'm still stuck on it. The goal is to read from a text file (Have that working), line by line (Not working). As it stands, this is my latest attempt:
With open("Product.txt","r") as file:
for i in file:
lineRead = file.readline()
productSplit = lineRead.split(",")
productNameList.append(productSplit[0])
productCostList.append(productSplit[1])
productPriceList.append(productSplit[2])
What I am trying to do:
Read the text file line-by-line.
Split the result on commas.
Assign values at specific indexes to specific lists.
I'm not really sure how to use readline, and I couldn't understand it from the documentation, nor the other questions. I think that's where my problem is. I'd appreciate being told what I'm doing wrong. And as a side note, could I read in the whole file, split on the new line, and then split those indexes on commas?
productNameList = []
productCostList = []
productPriceList = []
With open("Product.txt","r") as file:
for line in file:
productSplit = line.split(",")
productNameList += [productSplit[0]]
productCostList += [productSplit[1]]
productPriceList += [productSplit[2]]
I am facing a problem with the fwrite function from the DataTable package in R. In fact it appends the wrong way and I'd end up with something like:
**user ref version status Type DataExtraction**
user1 2.02E+11 1 Pending 1 No
user2 2.02E+11 1 Saved 2 No"user3" 2.01806E+11 1 Saved NB No
I am using the function as follows :
library(data.table)
fwrite(Save, "~/Downloads/Register.csv", append = TRUE, sep = ",", quote = TRUE)
Reproducible example:
fwrite(data.table(user="user3",
ref="204094093",
version="2",
status="Pending",
Type="1",DataExtraction="No"),
"~/Downloads/test.csv", sep = ",", append = FALSE)
fwrite(data.table(user="user3",
ref="204094093",
version="2",
status="Pending",
Type="1",DataExtraction="No"),
"~/Downloads/test.csv", sep = ",", append = TRUE)
I'm not sure if it isolates the problem, but it seems that if I manually change something in the .csv file (for instance rename DataExtraction to Extraction), the problem of appending in the wrong way occurs.
Does someone know what is going wrong?
Thanks!
When I run your example code I have no problems with the behavior - the file comes out as desired. Based on your comments about manually changing what is in the file, and what the undesired output looks like, here is what I believe is happening. When fwrite() (and many other similar IO functions) write to a file, each line has at the end of it a line break (in R, this is generally represented as \n). This is desired, so that subsequent lines of data indeed appear on subsequent lines of the file. Generally this will also mean that when you open a file in a text editor, there will be a blank line at the very end, since this reflects the line break in the last line that was written. (different editors handle this differently though). So, I suspect what is happening is that when you go in and manually edit the file in your editor, you are somehow losing that last line break. What this means is that when you go to write again using append, there is no line break at the end of the file, and therefore you get the undesired behavior of two lines of data on a single line of the file.
So, the solution would be to either find how to prevent your manual editing from deleting that last line break character. Barring that, there are ways to just write a single line break character to the file using R. E.g. with the cat() function.
Learning AppleScript I'm trying to practice and I wanted to see if I could get the count of a class in an .xhtml file.
In my BBEdit project I set a variable for the project with:
set this_project to file of project document 1
made sure to target all the .xhtml files with:
set total_xhtml to {search mode:grep, case sensitive:false, match words:false, extend selection:false, returning results:true, filter:{ID:"111232452", filter_mode:and_mode, filter_terms:{{operand:"xhtml", field:«constant ****FnSf», operator:op_is_equal}}}}
but when I try to count the class for each file I'm stumped..
I did try:
set varCount to count class=\"foobar\"" of (this_project in total_xhtml)
If I try set varCount to count class=\"foobar\"" it returns a number in the AppleScript Editor but how can I get the full count for each file in a project?
If I understand what you're asking, you want to find out how many times your class is listed in your different .xhtml files.
If that's what you're trying to accomplish, the below is a working example that should do what you're looking for.
on run
try
set foundItems to do shell script "grep -ri 'yourClassName' " & quoted form of "/Path/to/Your/directory"
on error
set foundItems to ""
end try
set oldDelims to AppleScript's text item delimiters -- save their current state
set AppleScript's text item delimiters to {return} -- declare new delimiters
set tempList to every text item of foundItems
set AppleScript's text item delimiters to oldDelims -- restore them
set foundCount to count of tempList
end run
Just realized I never accepted an answer to my question and I wanted to close out this Q&A. I didn't choose the provided answer because text item delimiters were firing an incorrect count from the file and I wanted an approach that didn't call on a do shell.
In AppleScript and BBEdit if you reference the Dictionary there is FIND:
and with a repeat loop I was able to step through each occurrence of class="foobar":
## beginning of file
select insertion point before first character of text document text document 1
set countClass to 0
repeat
## find pattern
set findClass to find "class=\"foobar\"" searching in text 1 of text document text document 1 options {options} with selecting match
## exit after no more patterns
if not found of findClass then exit repeat
## increment for every occurrence
set countClass to countClass + 1
end repeat
## return total count of pattern
return countClass
With the above repeat I was able to step through each file and total up every occurrence of class foobar. Hope this helps the next person.