Search and replace in file
In a file :
file.html
I want to change for example "hello" by "hi".
What can I do ?
Thanks
You'll have to read the file, one line at a time, and call the following method:
Returns a string with <b>oldStr</b> replaced by <b>newStr</b> as many times as indicated by the parameter <b>inst</b>.
public static String Substitute(String str1, String oldStr, String newStr, int inst) {
String tmp=str1;
int k=oldStr.length();
for (int i=1;i<=inst;i++) {
int j=tmp.indexOf(oldStr);
if (j<0) return tmp;
tmp=tmp.substring(0,j)+newStr+tmp.substring(j+k);
return tmp;
V.V.
Similar Messages
-
RH 11: Search and replace in files: options are grayed out
Hello to all,
just wanted to replace a string in my RH Project. Using the Dialog "search and replace in files", the Option "ignore HTML-Tags" is grayed out.
Even if i create a new, empty Project: the same Problem.
Is this Option generally not available in RH11 or what is the Trigger to gray the Option out?
Sorry if I dont hit the English terminology used in RH, I'm using the German Version...
Regards,
JulianHi Julian. This sounds like you have the Advanced options displayed. try going back to the Basic options by clicking the double arrow icon in the top right hand corner:
-
Q: Perl search and replace in file containing Japanese characters
Hello Perl experts
I have a Unicode Text file (saved from Excel) and would like to replace the tabs in that file with commas. I am using the following code but it seems to be changing some of the Japanese characters in the file only in certain files. If needed I can attach the original file but was wondering if there is a better way to do this using Perl only.
open INF, "ST_20120829_102433_AB.txt" ;
open OUTF, ">outf.txt" ;
binmode INF;
binmode OUTF;
while (read (INF, $readbuff, 65535)) {
$readbuff =~ s/$t1/,/g ;
print OUTF $readbuff ;
close INF ;
close OUTF ;
Thanks
ArindamOff topic. These are Java forums. Locking.
-
Search and replace for Unicode characters
Hello,
I have a function which searches and replaces characters on file. It works with ASCI sharacters, but not when the strings which needs to be replaced contains Unicode ('á', 'ā') etc. The source file is codding
utf8 .
$file = "file.txt"
$SearchReplace = @($file)
#Process files by performing a search and replace
foreach ($file in $SearchReplace)
#Select-Object -Skip 1 |
(Get-Content $file) |
Foreach-object { $_ -replace 'unicode_string' , ';' } |
out-file -encoding Unicode $file
How to get working the search (and replace) function with Unicode characters?
Thanks!No. it does not. I have verified that script does not recognize the diacritic
('á',
'ā') characters when at all the operations with the files I have specified encoding utf-8/Unicode. -
Search and replace strings in a file while ignoring substrings
Hi:
I have a java program that searches and replaces strings in a file. It makes a new copy of the file, searches for a string in the copy, replaces the string and renames the new copy to the original file, thereafter deleting the copy.
Now searching for "abcd", for eg works fine, but searching for "Topabcd" and replacing it doesnot work because there is a match for "abcd" in a different search scenario. How can I modify the code such that if "abcd" is already searched and replaced then it should not be searched for again or rather search for "abcd" as entire string and not if its a substring of another string.
In the below code output, all instances of "abcd" and the ones of "Topabcd" are replaced by ABCDEFG and TopABCDEF respectively, whereas according to the desired output, "abcd" should be replaced by ABCDEFG and "Topabcd" should be replaced by REPLACEMEFIRST.
try
String find_productstring = "abcd";
String replacement_productstring = "ABCDEFG";
compsXml = new FileReader(compsLoc);
compsConfigFile = new BufferedReader(compsXml);
File compsFile =new File("file.xml");
File compsNewFile =new File("file1.xml");
BufferedWriter out =new BufferedWriter(new FileWriter("file1.xml"));
while ((compsLine = compsConfigFile.readLine()) != null)
new_compsLine =compsLine.replaceFirst(find_productstring, replacement_productstring);
out.write(new_compsLine);
out.write("\n");
out.close();
compsConfigFile.close();
compsFile.delete();
compsNewFile.renameTo(compsFile);
catch (IOException e)
//since "Topabcd" contains "abcd", which is the search above and hence the string "Topabcd" is not replaced correctly
try
String find_producttopstring = "Topabcd";
String replacement_producttopstring = "REPLACEMEFIRST";
compsXml = new FileReader(compsLoc);
compsConfigFile = new BufferedReader(compsXml);
File compsFile =new File("file.xml");
File compsNewFile =new File("file1.xml");
BufferedWriter out =new BufferedWriter(new FileWriter("file1.xml"));
while ((compsLine = compsConfigFile.readLine()) != null)
new_compsLine =compsLine.replaceFirst(find_producttopstring, replacement_producttopstring);
out.write(new_compsLine);
out.write("\n");
out.close();
compsConfigFile.close();
compsFile.delete();
compsNewFile.renameTo(compsFile);
catch (IOException e)
}Thanks a lot!Hi:
I have a java program that searches and replaces
strings in a file. It makes a new copy of the file,
searches for a string in the copy, replaces the
string and renames the new copy to the original file,
thereafter deleting the copy.
Now searching for "abcd", for eg works fine, but
searching for "Topabcd" and replacing it doesnot work
because there is a match for "abcd" in a different
search scenario. How can I modify the code such that
if "abcd" is already searched and replaced then it
should not be searched for again or rather search for
"abcd" as entire string and not if its a substring of
another string.
In the below code output, all instances of "abcd" and
the ones of "Topabcd" are replaced by ABCDEFG and
TopABCDEF respectively, whereas according to the
desired output, "abcd" should be replaced by ABCDEFG
and "Topabcd" should be replaced by REPLACEMEFIRST.
try
String find_productstring = "abcd";
String replacement_productstring = "ABCDEFG";
compsXml = new FileReader(compsLoc);
compsConfigFile = new
BufferedReader(compsXml);
File compsFile =new File("file.xml");
File compsNewFile =new File("file1.xml");
BufferedWriter out =new BufferedWriter(new
FileWriter("file1.xml"));
while ((compsLine =
compsConfigFile.readLine()) != null)
new_compsLine
=compsLine.replaceFirst(find_productstring,
replacement_productstring);
out.write(new_compsLine);
out.write("\n");
out.close();
compsConfigFile.close();
compsFile.delete();
compsNewFile.renameTo(compsFile);
catch (IOException e)
//since "Topabcd" contains "abcd", which is
the search above and hence the string "Topabcd" is
not replaced correctly
try
String find_producttopstring = "Topabcd";
String replacement_producttopstring =
topstring = "REPLACEMEFIRST";
compsXml = new FileReader(compsLoc);
compsConfigFile = new
gFile = new BufferedReader(compsXml);
File compsFile =new File("file.xml");
File compsNewFile =new File("file1.xml");
BufferedWriter out =new BufferedWriter(new
dWriter(new FileWriter("file1.xml"));
while ((compsLine =
compsLine = compsConfigFile.readLine()) != null)
new_compsLine
new_compsLine
=compsLine.replaceFirst(find_producttopstring,
replacement_producttopstring);
out.write(new_compsLine);
out.write("\n");
out.close();
compsConfigFile.close();
compsFile.delete();
compsNewFile.renameTo(compsFile);
catch (IOException e)
Thanks a lot!I tried the matches(...) method but it doesnt seem to work.
while ((compsLine = compsConfigFile.readLine()) != null)
if(compsLine.matches(find_productstring))
System.out.println("Exact match is found for abcd");
new_compsLine =compsLine.replaceFirst(find_productstring, replacement_productstring);
out.write(new_compsLine);
out.write("\n");
else
System.out.println("Exact match is not found for abcd");
out.write(compsLine);
out.write("\n"); -
Bug in Multi-File Search and Replace in RH6?
I'm using the 6.0 trial, and one thing I want to be able to
do is to replace the variables I put in using JavaScript with the
new feature variables in 6.0. So, I went in to multi-file search
and replace, and searched for:
<script
language=JavaScript>document.write(varProduct)</script>
I copied this code directly from a topic, and pasted it into
the Search tool.
I don't even care whether I can successfully copy the code
from my new variable for use as the replacement text; I'd be happy
if the multi-file search would simply find all occurrences of my
original code so I don't miss any during conversion to 6.0.
It keeps coming up as not found, and this concerns me
greatly. Has anyone else experienced this? Is it a known bug? Does
anyone have a workaround?
Thanks!Hi robowriter
I'm assuming you are using the wondrous little applet known
as Multi-File Find and Replace.
Unfortunately, this little beastie is like a cat. It
frequently has a fussy tummy and loves to hork up furballs. Well,
not exactly. Really what it does is fail to properly handle
anything with a line break. So you need a tool that does do this.
Fortunately, one exists! It is called FAR (Find And Replace). Oddly
enough, it was also written by a fellow Microsoft Help MVP named
Rob Chandler. (Rob lives in Australia)
You can download a trial version of FAR from the FAR page.
Click here to visit
the FAR page
Cheers... Rick -
How to search and replace in an xml file using java
Hi all,
I am new to java and Xml Programming.
I have to search and replace a value Suresh with some other name in the below xml file.
Any help of code in java it is of great help,and its very urgent.
I am using java swings for generating two text boxes and a button but i am not able to search in the xml file thru the values that are entered into these text boxes.
Thanks in advance.
**XML File*
<?xml version="1.0" encoding="ISO-8859-1"?>
<student>
<stud_name>Suresh</stud_name>
<stud_age>40</stud_age>
</student>Also i am using SAX Parser in the java program
any help of code or any tutorials for sax parisng is very urgent please help me to resolve this problem
Edited by: Karthik84 on Aug 19, 2008 1:45 AM
Edited by: Karthik84 on Aug 19, 2008 3:15 AMUsing XPath to locate the elements you are after is very easy.
Try something like this:
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class BasicXMLReplaceWithDOM4J {
static String inputFile = "C:/student.xml";
static String outputFile = "C:/studentRenamed.xml";
public static void main(String[] args) throws Exception {
// Read xml and build a DOM document
Document doc = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(new InputSource(inputFile));
// Use XPath to find all nodes where student is named 'Suresh'
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList)xpath
.evaluate("//stud_name[text()='Suresh']", doc, XPathConstants.NODESET);
// Rename these nodes
for (int idx = 0; idx < nodes.getLength(); idx++) {
nodes.item(idx).setTextContent("Suresh-Renamed");
// Write the DOM document to the file
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform(new DOMSource(doc), new StreamResult(new File(outputFile)));
}- Roy -
Batch File Search and Replace a String
I've written a batch file to handle the copying of files. I'm trying to reduce the number of parameters that I pass to the batch file.
I want the batch file to perform a string replace on the directory path that is passed as a parameter to the batch file.
Here is how I run the batch file:
COPY_FILE.BAT C:\DATA\FTP_DATA\ACME\IN (I'm passing directory path "C:\DATA\FTP_DATA\ACME\IN" as parameter %1)
So my batch file will copy a file to the directory path represented by parameter %1.
But then I want the file copied to a second directory. I want the batch file to replace string "FTP_DATA" in the directory path with string "FTP_DATA\ARCHIVE"
I know I could pass this path as a second parameter, but I want to limit the number of parameters so figured I could have the batch file perform a search and replace on string "FTP_DATA".
After searching the internet, I thought I found the answer by using the following command in the batch file:
set ARCHIVE_PATH=%1:FTP_DATA=FTP_DATA\ARCHIVE
But it doesn't perform a search and replace. It just appends to the directory path with the following result:
C:\DATA\FTP_DATA\ACME\IN:FTP_DATA=FTP_DATA\ARCHIVE
What I want is the this:
C:\DATA\FTP_DATA\ARCHIVE\ACME\IN
Any suggestions?Your syntax is not correct. Example:
@echo off
setlocal enableextensions
set ARCHIVE_PATH=C:\DATA\FTP_DATA\ACME\IN
echo %ARCHIVE_PATH%
set ARCHIVE_PATH=%ARCHIVE_PATH:FTP_DATA=FTP_DATA\ARCHIVE%
echo %ARCHIVE_PATH%
endlocal
-- Bill Stewart [Bill_Stewart] -
Can i search and replace in the itunes library database file?
Hi --
I need to update the file location in the itunes database for a few thousand songs. These songs are just part of my full collection. A few hundred of these songs are referenced in playlists. Rebuilding the libarary, consolidating the library, adding the new folder to library -- none of these suggested solutions to similar problems will work for me. (My collection is too large for one drive and is spread over several drives, and I have many playlists with hundreds of songs, and the playlists would be ruined with any of these approaches.)
I thought I had an easy solution by doing a search and replace in "itunes library.xml," replacing the old file path with the new one. But then I learned (as you probably already know) that the real information is stored in "itunes library," and the xml file is just a downstream copy of that information. All my edits were overwritten as soon as I opened itunes.
The solution to my problem is completely straight-forward if I can just do a search and replace in "itunes libray." Is it possible to edit that file directly? The only database program I have is MS Access, on windows, and it couldn't open the file.
Thanks very much
- MarkHow to Move Your Music to a New Computer.
-
Multiple file search and replace
Does anyone know if it is possible to do search / replaces in script, text etc over multiple files? If not, is there any 3rd party software available to do it?
I have hundreds of files I have to make the same change in and just wanted to find out before I have to do the mind-numbing process of going through them all.
Any help will be greatly appreciated!Depending upon what type of files and what kind of changes you need to make, sure you can. You can do a google search for 'binary search and replace'; here's one of the first results, which searches for and replaces strings in multiple text and/or binary files:
http://gnuwin32.sourceforge.net/packages/gsar.htm -
Search and replace in Muse files
I have a lot of links to my own files in a given web page.
I will be relocating the files and want to know if I can run a search and replace the links with their new urls on the Muse files
I do not want to be manually replacing each link using the Muse add link button. That will take forever.
I will be using a separate search and replace program not anything within Muse
Clear?Hi J KIrby,
You have the option of 'Find and Replace' in Muse under 'Edit' menu option.
Also, please refer to our release notes page to see what other features have been added to muse in the last update - Release notes | Adobe Muse CC
- Abhishek Maurya -
Using a variable in a Powershell search and replace string
Hi
a couple of days ago I posted a question about doing a search and replace with wildcards
Search and repalce with Widcards
I got a swift and very helpful answer but now I need to build on it.
In a text file I wanted to replace all the text between two defined words. the script I got was this
$text = 'Some Server this bit of text varies Language stuff'
$text -replace '(.*Server) .+? (Language.*)','$1 it will always say this $2'
It works great but now I want to replace "it will always say this" with a variable and I can't figure out the correct grammar to make this happen.
Can anyone help??
Thanks
AlexHere's one way:
$replace = 'it will aways say this'if ( $text -match '(.*Server) .+? (Language.*)' )
{ "{0} $Replace {1}" -f $matches[1,2] }
else { $text }
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " " -
HTML character entities problem in saved regex search and replace query
I have a many search and replace regular expression queries (.dwr files) that I have saved. I have a problem specifically with saved queries that contain HTML entities such as "& nbsp ; " or "& shy ;" (spaces added otherwise code doesn't render in browser). For example if I use the following search:
([\d]{3}& shy ;[\d]{3}& shy ;[\d]{4}|[\d]{3}& nbsp ;[\d]{3}& nbsp ;[\d]{4})
(which searches for numbers in the 888-555-1234 or 888 555 1234 formats)
This will work fine if I manually enter it into the search text area. However if I save it to file and reload it, it will no longer work because the ­ and characters are now displayed as " " (space) and "-"(shy) rendering the saved query useless as it's no longer searching for the code. I have some fairly long and complex queries and this is becoming a problem.
Thanks for any help.
I'm currently using Dreaweaver CS4 at home and CS5.5 at work.Thanks for your reply Kenneth, but that is not what I'm trying to accomplish. I'm looking for the HTML entities that exist in the source code which are & shy ; and & nbsp ; (without the spaces). As I mentioned above, if I enter them manually in the search box, I will get the corrrect results. If I save the search and then reload it, the special characters are no longer in HTML and the search is now useless.
Just for example again
In an open document in code view insert a number in the format (without the spaces): 888& nbsp;888& nbsp ;8888
Open a search dialog box and enter (without the spaces): [\d]{3}& nbsp ;[\d]{3}& nbsp ;[\d]{4}
The search will find that entry.
Save search as phone.dwr for example. Then load it and try the search again. It won't work because upon loading the search Dreamweaver replaces the HTML code which was saved with the rendered HTML. So now the search shows up as: [\d]{3} [\d]{3} [\d]{4} which will not find the string with hard coded non-breaking spaces that I'm looking for.
Basically I want to be able to save a search query for reuse. When I load a search query, I want it to be exactly what I saved, not something that DW has rendered (that doesn't work). -
Find and replace in files does not refresh when changes are made
Using RH10, I search for a term with the Find and Replace Options pod. I use the Find Results list as a reference only, and open each file from the Topic List. I do this instead of opening the topics from the Find Results tab because I want to see the WYSIWYG changes. Anyway, I make my edits and delete the search term from each topic. Then, to make sure I have done all the work, I run the search again. I expect to see no or at least fewer matches than before, but I still get some or all of the same results as before, even though I double-check the Design and HTML views of the topics and even check the source of a topic separately in Notepad, all indicating that the search term is gone from the topic.
There is some ridiculous lingering or echo going on. I make sure to select Look in: <Current Project>, and the paths of all the Find Results files are fine. How do you refresh the search so that this echo doesn't occur?
Also, I close the Find and Replace Options pod, reopen it and start the search again, and the same results are there.
Message was edited by: DMilkesHi there
To avoid the message about the file being modified, close all topics in the editor before commencing with any find and replace operations. The message is produced because you have the aforementioned topics open in the editor.
As you mention "Find and replace in files", I'm guessing you are referring to version 8. I just opened version 8 and performed a find and replace to confirm. There is a button allowing you to clear all.
On the clicking and dragging to create a link, it's never worked that way. You simply cannot drag from the Topic List pod to do it. But you can from the Project Manager pod. Perhaps you should consider submitting a Wish Form to ask for the ability to click and drag from the Topic List pod to create links.
As far as rearranging your topics into different folders, as long as you do it from inside RoboHelp you shouldn't be destroying any links. RoboHelp will update references for you as you move things around. The only issue is if you are automatically creating your TOC structure based on the project structure. But usually that's a one time shot. From there forward you just manage things using the TOC editor.
Hopefully this helps... Rick
Helpful and Handy Links
RoboHelp Wish Form/Bug Reporting Form
Begin learning RoboHelp HTML 7 or 8 within the day - $24.95!
Adobe Certified RoboHelp HTML Training
SorcerStone Blog
RoboHelp eBooks -
How to get numeric result from 'search and replace' string function
hii
i am using search and replace function to get output.my output is of form ' *X0123.3 ' .here i am replacing the term *X01 with space because i need only 23.3 as a result.but i am unable to get that out in the output indicator.i used lot of functions like string to number and so on but i am getting output as zero .can you please suggest me wt to do.i am attaching a copy of my file.
Attachments:
Untitled 1.vi 25 KBYour problem is the fact that you are converting it to an integer, thus loosing the fractional part. You need to use "Fract/Exp String To Number" instead and create a DBL so yor data is 23.3 instead of 23.
You also don't need to manipulate the string if the initial part is of constant lenght. Just use the offset input as in the figure below.
Message Edited by altenbach on 07-31-2006 07:02 AM
LabVIEW Champion . Do more with less code and in less time .
Attachments:
ToDBL.png 2 KB
Maybe you are looking for
-
Client cannot accept invitation to Creative Cloud
Issue: An invitation was sent to a client on our Creative Cloud team. When they open the invitation and click the link, they get a page cannot be displayed. Products download and install, but are on a 30 day trial rather than our one year subscript
-
ITunes error 11111 when trying to purchase music
Hi when i click buy on a piece of music i get this message 11111 and im completely sure its nothing to do with firewall so is there actually any way of resolving this? only happened since ive had version 7.6 any clues to whats wrong and has anyone el
-
Is it possible to specify the connection that a certain Aplication module/transaction uses? I need to have a connection open for each user, thats's why i need to tell the application module the connection to use... Please this is very important... Da
-
Good day apex user, how can I make an item unique in apex 4 and it when insert the same data on the field which already exist and error message should appear?? regards,
-
Retroactive education rebate?? D'oh!
Hi there, I bought my first mac the other day (15 inch macbook pro with A LOT of customization) from the online apple store. I've used Apple products before (at work & school), but I've never actually bought one. So, unfortunately, when I went to buy