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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extended replacing of nonspecific strings in text files [beware complicated !]
# 1  
Old 06-02-2011
Extended replacing of nonspecific strings in text files [beware complicated !]

Well, to make another post at this helpful forum SmilieSmilie:

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]
Code:
 <?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]

Last edited by pasc; 06-02-2011 at 11:05 AM..
# 2  
Old 06-02-2011
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..
This User Gave Thanks to getmmg For This Post:
# 3  
Old 06-02-2011
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...
# 4  
Old 06-02-2011
This should do the the job looks kind of messy though..

Code:
 
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.

Code:
 
<?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>

This User Gave Thanks to getmmg For This Post:
# 5  
Old 06-02-2011
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)

Code:
 <?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 ?
# 6  
Old 06-02-2011
Quote:
Originally Posted by pasc
Seems, like a good approach, however not exactly what I was looking for.

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..

Code:
 
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

Code:
 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 Smilie.
This User Gave Thanks to getmmg For This Post:
# 7  
Old 06-02-2011
Ok, I will try your perl script later on my linux portion of my pc Smilie.

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
Code:
 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 Smilie.

Still, gotta love this forum, getting an answer to such a complex problem sure rocks ^^.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extended ASCII Characters keep on getting reintroduced to text files

I am working with a log file that I am trying to clean up by removing non-English ASCII characters. I am using Bash via Cygwin on Windows. Before I start I set: export LC_ALL=C I clean it up by removing all non-English ASCII characters with the following command; grep -v $''... (4 Replies)
Discussion started by: lewk
4 Replies

2. Shell Programming and Scripting

Replacing strings in various files

i'm trying to figure out the easiest way to replace a string: pineapple pineapple-reg basketball basketball-reg football foot-reg-ball i'm storing the above in a file called wordstoreplace.txt for each line above, the word in the first column is to be replaced by the word in the second... (4 Replies)
Discussion started by: SkySmart
4 Replies

3. Shell Programming and Scripting

Finding a text in files & replacing it with unique strings

Hallo Everyone. I have to admit I'm shell scripting illiterate . I need to find certain strings in several text files and replace each of the string by unique & corresponding text. I prepared a csv file with 3 columns: <filename>;<old_pattern>;<new_pattern> ... (5 Replies)
Discussion started by: gordom
5 Replies

4. Shell Programming and Scripting

Finding/replacing strings in some files based on a file

Hi, We have a file (e.g. a .csv file, but could be any other format), with 2 columns: the old value and the new value. We need to modify all the files within the current directory (including subdirectories), so find and replace the contents found in the first column within the file, with the... (9 Replies)
Discussion started by: Talkabout
9 Replies

5. Shell Programming and Scripting

Replacing variable Text between fixed strings

Hello all, This is my first post and I hope you can help me out. I searched for quite some hours now and haven't found a simple solution to my problem. It is as following: I got this file: dl.dropbox.com/u/14586156/stuff/Bookmarks.plist and want to replace the Text between... (9 Replies)
Discussion started by: pasc
9 Replies

6. Shell Programming and Scripting

Search complicated strings on file

Can someone help me? I been figuring out how I can search and extract a complicated search string from a file. The whole string is delimited by a period. And the file where I'm searching is composed of differnt string such as that. For example, I have this search string: and I have a file... (3 Replies)
Discussion started by: Orbix
3 Replies

7. Shell Programming and Scripting

Text strings in files.

Hi all I have two files X.txt and Y.txt. The file format of X.txt is : madras is also the fountainhead of the theosophical movement which spread worldwide . and second file Y.txt is of the format: madra|s|nsubj is|cop also|advmod the|det fountainhead|empty of|prep the|det... (3 Replies)
Discussion started by: my_Perl
3 Replies

8. Shell Programming and Scripting

replacing strings with text from other file

Hi, Im trying to update some properties files with text from another file: file1 user=xyz file2 user= after script file2 user=xyz Im using this reading the $QUARTZURL,ETC... from quartz.properties: echo... (1 Reply)
Discussion started by: mc1392
1 Replies

9. Shell Programming and Scripting

Find information from complicated strings

Hi experts, I have the file with these lines: var1=thu_13:12:32,var2=Microsoft,var3=240ms,var4=Mozilla/4.0_(sun;_MSIR_3-4;_windows.;_NET_1.1323.53 var1=thu_13:13:32,var2=Microsoft,var3=213ms,var4=Mozilla/4.0_(sun;_MSIR_3-4;_windows.;_NET_1.1323.53... (9 Replies)
Discussion started by: lalelle
9 Replies

10. Shell Programming and Scripting

Replacing lines in text files

Hi, I have 2 sets of text files. I need to take a field from a certain line in set 1 and put it in the same place in set b. The line appears once per file, in different places but is a set format and has the unique word "ANTENNA" in it and is always 81 characters long. Example from set a: ... (7 Replies)
Discussion started by: Jonny2Vests
7 Replies
Login or Register to Ask a Question