need to delete the last occurence of a string i.e. VirtualHost in a file. I can do it using java, shell. The file structure is an xml file. What I need is I need to shift the VirtualHost to the end. So I am trying to remove it and then append.
Current Text:
</VirtualHost>
#Added for Patch
<LocationMatch ^/bea_wls_internal/>
RewriteOptions inherit
</LocationMatch>
Desired Text:
#Added for Patch
<LocationMatch ^/bea_wls_internal/>
RewriteOptions inherit
</LocationMatch>
</VirtualHost>
i tried to do this:
int c=1,i=1;
int loc[]=new int[10];
//String str="</VirtualHost>";
BufferedReader br= new BufferedReader (new FileReader ());
String line=br.readLine();
FileWriter fw=new FileWriter(file_location);
PrintWriter pw=new PrintWriter(fw);
List<String> lines = new ArrayList<String>();
while ((line != null)){
//System.out.println(line);
lines.add(line);
c++;
line=br.readLine();
}
br.close();
int size=lines.size();
System.out.println("size "+size);
while(c>i){
int filesize=lines.size();
System.out.println(filesize);
String str=lines.get(filesize-i);
System.out.println(str);
lines.remove(filesize-i);
if (!lines.equals("</VirtualHost>")){
System.out.println("inside if");
lines.add(str);
//break;
}
else if(lines.equals("</VirtualHost>")){
break;
}
i++;
}
for (String writeLine : lines)
pw.println(writeLine);
pw.append("</VirtualHost>");
pw.flush();
pw.close();
fw.close();
System.out.println(c);
}
}