Replacing variable Text between fixed strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing variable Text between fixed strings
# 1  
Old 05-07-2011
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 "file://localhost/ and /Documents/ with whatever. (Not literaly whatever, just any other text).
( in BASH/SHELL)


Since this is not a normal sorted text file and instead a plist it seems a quite Smilie task to do.

I guess sed and gawk should be able to do this, yet neither of the solution provided anywhere helped me to accomplish this

I really hope anyone could help me with this since it is driving me crazy.


Thanks in advance for helping out a desperate shell scripting newb:

pasc
BTW: if there is a solution: Can these replacements also be limited to the ocurrences (e.g. only replace the first, second or maybe third occurence ?

Also: Is there any plist parsing utility for shell or awk that can write the parsed result to a new file in easily readable format ?
# 2  
Old 05-07-2011
Code:
sed -r 's/(file:\/\/localhost\/).*(\/Documents\/)/\1wathever\2/g' Bookmarks.plist > out.txt

This User Gave Thanks to tukuyomi For This Post:
# 3  
Old 05-07-2011
Try:
Code:
sed 's#file://localhost/.*/Documents/#whatever#' Bookmarks.plist

This User Gave Thanks to bartus11 For This Post:
# 4  
Old 05-07-2011
Perfect Smilie Thanks for the quick answers.

It works!

if I wanted to do the following:

Do the above, but instead of the "whatever"
read a file in /var/mobile/ called "RELINKING.txt" and paste the first line of that file instead of "whatever"

?

Thanks again, you guys are awesome SmilieSmilie
# 5  
Old 05-07-2011
Code:
x=`head -1 /var/mobile/RELINKING.txt`; sed "s#file://localhost/.*/Documents/#$x#" Bookmarks.plist

This User Gave Thanks to bartus11 For This Post:
# 6  
Old 05-07-2011
Also worked Smilie

Now for my last two questions on this topic:

1. Is there a way to apply this only to the first, second whatever occurence of finding the strings localhost and Documents in the file ? (not all inisde the file ?)

2. (unlikely), is there a plist parser for shell or awk ?
# 7  
Old 05-07-2011
Code:
x=`head -1 /var/mobile/RELINKING.txt`; perl -pse '$n++<3 && s#file://localhost/.*/Documents/#$x#' -- -x=$x Bookmarks.plist

Change red "3" to the number of occurences you want to replace (assuming that only one "file://localhost..." entry is present per line).
This User Gave Thanks to bartus11 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using printf (or other?) to create variable fixed width text

I would like to use printf (or something else?) to create a line of text that has varying column widths. This will be used to create a fixed width file (with varying column widths). For example, consider variables $1 $2 $3 are equal to a, b, c respectively and they should be printed in column... (10 Replies)
Discussion started by: farrenthorpe
10 Replies

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

3. Shell Programming and Scripting

Insert a variable to a text file after fixed number of lines

Hi, I am new to unix. I need to insert a variable which contains some lines of text into a text file after fixed number of lines.. Please help me on this.. Thanks in Advance, Amrutha (3 Replies)
Discussion started by: amr89
3 Replies

4. Shell Programming and Scripting

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 =---- replace with: first... (6 Replies)
Discussion started by: pasc
6 Replies

5. Shell Programming and Scripting

Replacing strings

The code below gives the string "test1.txt" even though "tessdsdt" does not match "test1.txt". I would like to return "" if there is no match and return some kind of error that I can capture and decide what to do. echo test1.txt | awk -v src="tessdsdt" -v dst="test" '{sub(src,dst); print}' (16 Replies)
Discussion started by: kristinu
16 Replies

6. Shell Programming and Scripting

changing a variable length text to a fixed length

Hi, Can anyone help with a effective solution ? I need to change a variable length text field (between 1 - 18 characters) to a fixed length text of 18 characters with the unused portion, at the end, filled with spaces. The text field is actually field 10 of a .csv file however I could cut... (7 Replies)
Discussion started by: dc18
7 Replies

7. Shell Programming and Scripting

Replacing strings

I am trying to take the two line version of this: mv myFile.txt myFile.txt.bak sed 's/foo/bar/g' myFile.txt.bak > myFile.txt and make it into a shell script with three parameters. First two parameters are the string and string replacement and the third is file. So far this is what I have... (5 Replies)
Discussion started by: gordonheimer
5 Replies

8. Shell Programming and Scripting

Replace fixed strings only

Hi All, I just need to do find and replace in a file.... say for eg I have the input file like below: in.txt ##### oldtextoldtext oldtext oldtext oldtext oldtext123 oldtext- oldtext I need to replace oldtext to newtext... my output file should come like below.. out.txt... (9 Replies)
Discussion started by: askumarece
9 Replies

9. Shell Programming and Scripting

Help in replacing text with the value of a variable

Input file - tmp <begin> ./00003/ ./00004/ <end> I would like to replace "." with the value of pwd so that the output will look like /dev/project/00003/ t=`pwd` sed -e "s/\./$t/g" tmp > tmp1; The above piece of code is not working. Appreciate your help. (4 Replies)
Discussion started by: lotto_123
4 Replies

10. 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
Login or Register to Ask a Question