Extended replacing of nonspecific strings in text files [beware complicated !]

Well, to make another post at this helpful forum :b::D:

I recently tried something like this, I want to replace all those numberings/letters that are located
between <string>file://localhost/var/mobile/Applications/ and /Documents/</string>

numberings =[Number/Letter]-[Number/Letter]-[Number/Letter]-[Number/Letter]-[Number/Letter]

replace with:

  • first instance with 1234
  • scnd instance with 1234
  • thrd instance with 5678
  • fourth instance with 5678
  • fifths instance with 1010
  • sithth instance with 1010
  • seventh instance with 1111
  • eighth instance with 1111

[I marked them down in the example: ]

The text file:
[spoiler]

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Children</key>
  <array>
    <dict>
      <key>URIDictionary</key>
      <dict>
        <key></key>
        <string>file://localhost/var/mobile/Media/Downloads/</string>
        <key>title</key>
        <string>Downloads</string>
      </dict>
      <key>URLString</key>
      <string>file://localhost/var/mobile/Media/Downloads/</string>
      <key>WebBookmarkType</key>
      <string>WebBookmarkTypeLeaf</string>
      <key>WebBookmarkUUID</key>
      <string>FCA20256-B43E-4B7B-B496-116EAB4F0937</string>
    </dict>
    <dict>
      <key>URIDictionary</key>
      <dict>
        <key></key>
        <string>file://localhost/var/mobile/Applications/453F6009-876D-4757-8111-2201555C96C3/Documents/</string>
        <key>title</key>
        <string>iCab Downloads</string>
      </dict>
      <key>URLString</key>
      <string>file://localhost/var/mobile/Applications/453F6009-876D-4757-8111-2201555C96C3/Documents/</string>
      <key>WebBookmarkType</key>
      <string>WebBookmarkTypeLeaf</string>
      <key>WebBookmarkUUID</key>
      <string>DC9D8A8C-B0F0-4E13-8F8B-167BBF9DE0C5</string>
    </dict>
    <dict>
      <key>URIDictionary</key>
      <dict>
        <key></key>
        <string>file://localhost/var/mobile/Applications/FC972FD1-DA09-4FD5-9BDE-3B292946DA36/Documents/</string>
        <key>title</key>
        <string>iDownloads</string>
      </dict>
      <key>URLString</key>
      <string>file://localhost/var/mobile/Applications/FC972FD1-DA09-4FD5-9BDE-3B292946DA36/Documents/</string>
      <key>WebBookmarkType</key>
      <string>WebBookmarkTypeLeaf</string>
      <key>WebBookmarkUUID</key>
      <string>EDBE01E6-14BC-408A-8D67-85C4E37E4074</string>
    </dict>
    <dict>
      <key>URIDictionary</key>
      <dict>
        <key></key>
        <string>file://localhost/var/mobile/Applications/3FFE1C4B-F8B4-483E-AACB-324718DEA9E4/Documents/</string>
        <key>title</key>
        <string>wikiamo</string>
      </dict>
      <key>URLString</key>
      <string>file://localhost/var/mobile/Applications/3FFE1C4B-F8B4-483E-AACB-324718DEA9E4/Documents/</string>
      <key>WebBookmarkType</key>
      <string>WebBookmarkTypeLeaf</string>
      <key>WebBookmarkUUID</key>
      <string>734D7530-6A0D-4F8F-A05D-0AA084E4C0F4</string>
    </dict>
    <dict>
      <key>URIDictionary</key>
      <dict>
        <key></key>
        <string>file://localhost/var/mobile/Applications/3B26CD6B-6E8B-4AB6-9A33-7ED1A2818FC1/Documents/</string>
        <key>title</key>
        <string>Manga BDR</string>
      </dict>
      <key>URLString</key>
      <string>file://localhost/var/mobile/Applications/3B26CD6B-6E8B-4AB6-9A33-7ED1A2818FC1/Documents/</string>
      <key>WebBookmarkType</key>
      <string>WebBookmarkTypeLeaf</string>
      <key>WebBookmarkUUID</key>
      <string>FCB13D18-D02B-4864-B62F-B2CF3B5606CA</string>
    </dict>
      <dict>
        <key></key>
        <string>file://localhost/var/mobile/Media/Documents/</string>
        <key>title</key>
        <string>Documents</string>
      </dict>
      <key>URLString</key>
      <string>file://localhost/var/mobile/Media/Documents/</string>
      <key>WebBookmarkType</key>
      <string>WebBookmarkTypeLeaf</string>
      <key>WebBookmarkUUID</key>
      <string>DC8D8A8C-B0R0-4E13-8F8T-167BBF9DE0C5</string>
    </dict>
  </array>
  <key>WebBookmarkFileVersion</key>
  <integer>1</integer>
  <key>WebBookmarkType</key>
  <string>WebBookmarkTypeList</string>
  <key>WebBookmarkUUID</key>
  <string>iFileBookmarkRoot</string>
</dict>
</plist>
 

[/spoiler]

Tell me if my undestanding is correct,
For ex I1=12,I2=23,I3=34,I4=45,I5=56 is the key.

When the pattern with N-N-N-N-N is found
do you want to replace it with I1-I2-I3-I4-I5?

or First matching pattern should be replace with I1-I1-I1-I1-I1, then with I2-I2-I2-I2-I2 and so on..

1 Like

I just added something to the text file, please reread it,

It seems your understanding is correct,
when N-N-N-N-N is found it should be replaced and each found instance with the said up there.

that means:

first instance N-N-N-N-N replace with 1234
second instance N-N-N-N-N replace ALSO with 1234

third instance N-N-N-N-N replace with 5678
fourth instance N-N-N-N-N replace ALSO with 5678
and so forth...

This should do the the job looks kind of messy though..

 
perl -ane 'BEGIN{@arr=("1234","5678","91011","1213");$i=0;$total=scalar(@arr)-1;$str} if($_=~/(\w+\-\w+\-\w+\-\w+\-\w+)/){if($curr ne $1){$curr = $1; $str = "$arr[$i]-$arr[$i]-$arr[$i]-$arr[$i]-$arr[$i]"; $_=~s/$1/$str/; $i++;$i = 0 if ($i >$total)}else{$_=~s/$1/$str/}} print $_' Input File

I have tried to replace Instance 1 with 1234, 2 with 5678 and so on. I have got this output.

 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Children</key>
  <array>
    <dict>
      <key>URIDictionary</key>
      <dict>
        <key></key>
        <string>file://localhost/var/mobile/Media/Downloads/</string>
        <key>title</key>
        <string>Downloads</string>
      </dict>
      <key>URLString</key>
      <string>file://localhost/var/mobile/Media/Downloads/</string>
      <key>WebBookmarkType</key>
      <string>WebBookmarkTypeLeaf</string>
      <key>WebBookmarkUUID</key>
      <string>1234-1234-1234-1234-1234</string>
    </dict>
    <dict>
      <key>URIDictionary</key>
      <dict>
        <key></key>
        <string>file://localhost/var/mobile/Applications/5678-5678-5678-5678-5678/Documents/</string>
        <key>title</key>
        <string>iCab Downloads</string>
      </dict>
      <key>URLString</key>
      <string>file://localhost/var/mobile/Applications/5678-5678-5678-5678-5678/Documents/</string>
      <key>WebBookmarkType</key>
      <string>WebBookmarkTypeLeaf</string>
      <key>WebBookmarkUUID</key>
      <string>91011-91011-91011-91011-91011</string>
    </dict>
    <dict>
      <key>URIDictionary</key>
      <dict>
        <key></key>
        <string>file://localhost/var/mobile/Applications/1213-1213-1213-1213-1213/Documents/</string>
        <key>title</key>
        <string>iDownloads</string>
      </dict>
      <key>URLString</key>
      <string>file://localhost/var/mobile/Applications/1213-1213-1213-1213-1213/Documents/</string>
      <key>WebBookmarkType</key>
      <string>WebBookmarkTypeLeaf</string>
      <key>WebBookmarkUUID</key>
      <string>1234-1234-1234-1234-1234</string>
    </dict>
    <dict>
      <key>URIDictionary</key>
      <dict>
        <key></key>
        <string>file://localhost/var/mobile/Applications/5678-5678-5678-5678-5678/</string>
        <key>title</key>
        <string>wikiamo</string>
      </dict>
      <key>URLString</key>
      <string>file://localhost/var/mobile/Applications/5678-5678-5678-5678-5678/</string>
      <key>WebBookmarkType</key>
      <string>WebBookmarkTypeLeaf</string>
      <key>WebBookmarkUUID</key>
      <string>91011-91011-91011-91011-91011</string>
    </dict>
    <dict>
      <key>URIDictionary</key>
      <dict>
        <key></key>
        <string>file://localhost/var/mobile/Applications/1213-1213-1213-1213-1213/Documents/</string>
        <key>title</key>
        <string>Manga BDR</string>
      </dict>
      <key>URLString</key>
      <string>file://localhost/var/mobile/Applications/1213-1213-1213-1213-1213/Documents/</string>
      <key>WebBookmarkType</key>
      <string>WebBookmarkTypeLeaf</string>
      <key>WebBookmarkUUID</key>
      <string>1234-1234-1234-1234-1234</string>
    </dict>
      <dict>
        <key></key>
        <string>file://localhost/var/mobile/Media/Documents/</string>
        <key>title</key>
        <string>Documents</string>
      </dict>
      <key>URLString</key>
      <string>file://localhost/var/mobile/Media/Documents/</string>
      <key>WebBookmarkType</key>
      <string>WebBookmarkTypeLeaf</string>
      <key>WebBookmarkUUID</key>
      <string>5678-5678-5678-5678-5678</string>
    </dict>
  </array>
  <key>WebBookmarkFileVersion</key>
  <integer>1</integer>
  <key>WebBookmarkType</key>
  <string>WebBookmarkTypeList</string>
  <key>WebBookmarkUUID</key>
  <string>iFileBookmarkRoot</string>
</dict>
</plist>

1 Like

Seems, like a good approach, however not exactly what I was looking for.

To clarify more:

  • ONLY the numberings BETWEEN<string>file://localhost/var/mobile/Applications/ and the /Documents/</string> should be replaced

in the end it should look like this:
(however, your approach with the 1234-1234-1234-1234-1234 etc. would be fine aswell)

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Children</key>
  <array>
    <dict>
      <key>URIDictionary</key>
      <dict>
        <key></key>
        <string>file://localhost/var/mobile/Media/Downloads/</string>
        <key>title</key>
        <string>Downloads</string>
      </dict>
      <key>URLString</key>
      <string>file://localhost/var/mobile/Media/Downloads/</string>
      <key>WebBookmarkType</key>
      <string>WebBookmarkTypeLeaf</string>
      <key>WebBookmarkUUID</key>
      <string>FCA20256-B43E-4B7B-B496-116EAB4F0937</string>
    </dict>
    <dict>
      <key>URIDictionary</key>
      <dict>
        <key></key>
        <string>file://localhost/var/mobile/Applications/1234/Documents/</string>
        <key>title</key>
        <string>iCab Downloads</string>
      </dict>
      <key>URLString</key>
      <string>file://localhost/var/mobile/Applications/1234/Documents/</string>
      <key>WebBookmarkType</key>
      <string>WebBookmarkTypeLeaf</string>
      <key>WebBookmarkUUID</key>
      <string>DC9D8A8C-B0F0-4E13-8F8B-167BBF9DE0C5</string>
    </dict>
    <dict>
      <key>URIDictionary</key>
      <dict>
        <key></key>
        <string>file://localhost/var/mobile/Applications/5678/Documents/</string>
        <key>title</key>
        <string>iDownloads</string>
      </dict>
      <key>URLString</key>
      <string>file://localhost/var/mobile/Applications/5678/Documents/</string>
      <key>WebBookmarkType</key>
      <string>WebBookmarkTypeLeaf</string>
      <key>WebBookmarkUUID</key>
      <string>EDBE01E6-14BC-408A-8D67-85C4E37E4074</string>
    </dict>
    <dict>
      <key>URIDictionary</key>
      <dict>
        <key></key>
        <string>file://localhost/var/mobile/Applications/1010/Documents/</string>
        <key>title</key>
        <string>wikiamo</string>
      </dict>
      <key>URLString</key>
      <string>file://localhost/var/mobile/Applications/1010/Documents/</string>
      <key>WebBookmarkType</key>
      <string>WebBookmarkTypeLeaf</string>
      <key>WebBookmarkUUID</key>
      <string>734D7530-6A0D-4F8F-A05D-0AA084E4C0F4</string>
    </dict>
    <dict>
      <key>URIDictionary</key>
      <dict>
        <key></key>
        <string>file://localhost/var/mobile/Applications/1111/Documents/</string>
        <key>title</key>
        <string>Manga BDR</string>
      </dict>
      <key>URLString</key>
      <string>file://localhost/var/mobile/Applications/1111/Documents/</string>
      <key>WebBookmarkType</key>
      <string>WebBookmarkTypeLeaf</string>
      <key>WebBookmarkUUID</key>
      <string>FCB13D18-D02B-4864-B62F-B2CF3B5606CA</string>
    </dict>
      <dict>
        <key></key>
        <string>file://localhost/var/mobile/Media/Documents/</string>
        <key>title</key>
        <string>Documents</string>
      </dict>
      <key>URLString</key>
      <string>file://localhost/var/mobile/Media/Documents/</string>
      <key>WebBookmarkType</key>
      <string>WebBookmarkTypeLeaf</string>
      <key>WebBookmarkUUID</key>
      <string>DC8D8A8C-B0R0-4E13-8F8T-167BBF9DE0C5</string>
    </dict>
  </array>
  <key>WebBookmarkFileVersion</key>
  <integer>1</integer>
  <key>WebBookmarkType</key>
  <string>WebBookmarkTypeList</string>
  <key>WebBookmarkUUID</key>
  <string>iFileBookmarkRoot</string>
</dict>
</plist>

Sorry if I confused you, I didn't meant to...
Also, I'm not sure if my platform supports perl, I surmise not, would this be doable in awk or simple shell aswell ?

This snippet below should do the job for you..

 
perl -ane 'BEGIN{@arr=("1234","5678","91011","1213");$i=0;$total=scalar(@arr)-1;$str} if($_=~/\/(\w+\-\w+\-\w+\-\w+\-\w+)/)
{if($curr ne $1){$curr = $1; $str = "$arr[$i]-$arr[$i]-$arr[$i]-$arr[$i]-$arr[$i]"; $_=~s/$1/$str/; $i++;$i = 0 if ($i >$total)}else{$_=~s/$1/$str/}} print $_'  Input
 diff Input output
26c26
<         <string>file://localhost/var/mobile/Applications/453F6009-876D-4757-8111-2201555C96C3/Documents/</string>
---
>         <string>file://localhost/var/mobile/Applications/1234-1234-1234-1234-1234/Documents/</string>
31c31
<       <string>file://localhost/var/mobile/Applications/453F6009-876D-4757-8111-2201555C96C3/Documents/</string>
---
>       <string>file://localhost/var/mobile/Applications/1234-1234-1234-1234-1234/Documents/</string>
41c41
<         <string>file://localhost/var/mobile/Applications/FC972FD1-DA09-4FD5-9BDE-3B292946DA36/Documents/</string>
---
>         <string>file://localhost/var/mobile/Applications/5678-5678-5678-5678-5678/Documents/</string>
46c46
<       <string>file://localhost/var/mobile/Applications/FC972FD1-DA09-4FD5-9BDE-3B292946DA36/Documents/</string>
---
>       <string>file://localhost/var/mobile/Applications/5678-5678-5678-5678-5678/Documents/</string>
56c56
<         <string>file://localhost/var/mobile/Applications/3FFE1C4B-F8B4-483E-AACB-324718DEA9E4/</string>
---
>         <string>file://localhost/var/mobile/Applications/91011-91011-91011-91011-91011/</string>
61c61
<       <string>file://localhost/var/mobile/Applications/3FFE1C4B-F8B4-483E-AACB-324718DEA9E4/</string>
---
>       <string>file://localhost/var/mobile/Applications/91011-91011-91011-91011-91011/</string>
71c71
<         <string>file://localhost/var/mobile/Applications/3B26CD6B-6E8B-4AB6-9A33-7ED1A2818FC1/Documents/</string>
---
>         <string>file://localhost/var/mobile/Applications/1213-1213-1213-1213-1213/Documents/</string>
76c76
<       <string>file://localhost/var/mobile/Applications/3B26CD6B-6E8B-4AB6-9A33-7ED1A2818FC1/Documents/</string>
---
>       <string>file://localhost/var/mobile/Applications/1213-1213-1213-1213-1213/Documents/</string>

Unfortunately I dont know awk :(.

1 Like

Ok, I will try your perl script later on my linux portion of my pc :).

Thanks a ton ! (Just out of curiosity, since perl is the only thing you coded and you can't code in awk, is a sed shell operation viable here ? I guess no, but still xD)

Like I said, the script provided won't fix my problem (since its written in perl), however I found another way to get this fixed:

I found out the lines of the plist that need to be changed and then used this:

53 = line number
VARIABLE = Text that will completly replace the line

 sed --in-place "53cVARIABLE" Bookmarks.plist

That works really well, because the line positions won't change in my file and I just replace them as whole :).

Still, gotta love this forum, getting an answer to such a complex problem sure rocks ^^.