0

I made an array of filenames of files in which match an pattern:

lista=($(grep -El "<LastVisitedURL>.+</LastVisitedURL>.*<FavoriteTopic>0</FavoriteTopic>" *))

Now I would delete in a file index.xml all tags enclosure which contains the filenames in the array.

for e in ${lista[*]}
do
  sed '/\<TopicKey FileName=\"$e\"\>.*\<\/TopicKey\>/d' index.xml
done

The complete script is:

#! /bin/bash

#search xml files watched and no favorites.
lista=($(grep -El "<LastVisitedURL>.+</LastVisitedURL>.*<FavoriteTopic>0</FavoriteTopic>" *))
#declare -p lista
for e in ${lista[*]}
do
  sed '/<TopicKey FileName=\"$e\">.*<\/TopicKey>/d' index.xml
done

Even though the regex pattern doesn't work, -i option in sed for edit in place index.xml, reload index file many times how filenames have the array, and this is bad.

Any suggestions?

ThunderFrame
  • 9,352
  • 2
  • 29
  • 60
chuy
  • 15
  • 3

1 Answers1

0

Here an example using in a shell :

% cat file.xml
<?xml version="1.0"?>                                                                                                     
<root>
<foobar>aaa</foobar>
<LastVisitedURL>http://foo.bar/?a=1</LastVisitedURL>
<LastVisitedURL>http://foo.bar/?a=2</LastVisitedURL>
<LastVisitedURL>http://foo.bar/?a=3</LastVisitedURL>
</root>

Then, the command line :

% xmlstarlet edit --delete '//LastVisitedURL' file.xml                                                             
<?xml version="1.0"?>
<root>
  <foobar>aaa</foobar>
</root>
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223