Replacing text in between the string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing text in between the string
# 1  
Old 07-05-2013
Replacing text in between the string

Here is just a few variations that follow the same principle. We need to update the path to all images (only images)

Code:
<IMG src="/3251_8832613.jpg">
<IMG src="https://www.unix.com/images/1303_4703925.jpg">
<IMG src="\\SERVER\DATA\Image\1948_4703925.jpg">
<img src="\\SERVER\STORE\image\2721_7990660.jpg">
<IMG style="HEIGHT: 3.5in" src="\\server\data\image\90417_4302013100410.jpg">
<IMG style="HEIGHT: 3.5in" src="\\10.10.10.10\data\image\90417_4302013100411.jpg">
<IMG style="WIDTH: 434px; HEIGHT: 369px" height=433 src="/2360_7990660.jpg" width=438>
<IMG style="WIDTH: 3.5in" src="https://66.66.66.66/images/5942_7990660.jpg">

The goal is to identify anything between <IMG .* > and replace the source path for the images src="" until there is a / or \ to this type of output, leaving actual filename in tact, only uniforming the path to where they are located.

Code:
<IMG src="https://www.unix.com/images/3251_8832613.jpg">
<IMG src="https://www.unix.com/images/1303_4703925.jpg">
<img src="https://www.unix.com/images/1948_4703925.jpg">
<img src="https://www.unix.com/images/2721_7990660.jpg">
<IMG style="HEIGHT: 3.5in" src="https://www.unix.com/images/90417_4302013100410.jpg">
<IMG style="HEIGHT: 3.5in" src="https://www.unix.com/images/90417_4302013100411.jpg">
<IMG style="WIDTH: 434px; HEIGHT: 369px" height=433 src="https://www.unix.com/images/2360_7990660.jpg" width=438>
<IMG style="WIDTH: 3.5in" src="https://www.unix.com/images/5942_7990660.jpg">

Thanks in advance as I've been out of ideas on how to get this pattern in awk, sed, grep...

DJ
Moderator's Comments:
Mod Comment Please use CODE tags (not ICODE tags) for multi-line code, input, and output samples.

Last edited by Don Cragun; 07-05-2013 at 09:35 PM.. Reason: ICODE tags -> CODE tags
# 2  
Old 07-05-2013
Try:
Code:
sed 'sXsrc=".*[\\/]Xsrc="https://www.unix.com/images/X' file

# 3  
Old 07-05-2013
Don,

it almost worked, however there are some paths like this that actually got completely removed:

Code:
<IMG src="/2531_4703925.jpg"><BR><IMG src="/2531_7990660.jpg"><BR></P>

After running sed above:


Code:
<P><IMG src = " images/P>


P.S. argh why does it replace the images/p with this: "https://www.unix.com/images/P ? I've tried to edit this post few times already. this time adding spaces after src = and "
# 4  
Old 07-05-2013
Try this:
Code:
sed '/^<[Ii][Mm][Gg]/sXsrc="[!"]*[\\/]Xsrc="https://www.unix.com/images/Xg' file

When file contains:
Code:
<IMG src="/3251_8832613.jpg">
<IMG src="https://www.unix.com/images/1303_4703925.jpg">
<IMG src="\\SERVER\DATA\Image\1948_4703925.jpg">
<img src="\\SERVER\STORE\image\2721_7990660.jpg">
<IMG style="HEIGHT: 3.5in" src="\\server\data\image\90417_4302013100410.jpg">
<IMG style="HEIGHT: 3.5in" src="\\10.10.10.10\data\image\90417_4302013100411.jpg">
<IMG style="WIDTH: 434px; HEIGHT: 369px" height=433 src="/2360_7990660.jpg" width=438>
<IMG style="WIDTH: 3.5in" src="https://66.66.66.66/images/5942_7990660.jpg">
<IMG src="/2531_4703925.jpg"><BR><IMG src="/2531_7990660.jpg"><BR></P

the output produced is:
Code:
<IMG src="https://www.unix.com/images/3251_8832613.jpg">
<IMG src="https://www.unix.com/images/1303_4703925.jpg">
<IMG src="https://www.unix.com/images/\SERVER\DATA\Image\1948_4703925.jpg">
<img src="https://www.unix.com/images/\SERVER\STORE\image\2721_7990660.jpg">
<IMG style="HEIGHT: 3.5in" src="https://www.unix.com/images/\server\data\image\90417_4302013100410.jpg">
<IMG style="HEIGHT: 3.5in" src="https://www.unix.com/images/\10.10.10.10\data\image\90417_4302013100411.jpg">
<IMG style="WIDTH: 434px; HEIGHT: 369px" height=433 src="https://www.unix.com/images/2360_7990660.jpg" width=438>
<IMG style="WIDTH: 3.5in" src="https://66.66.66.66/images/5942_7990660.jpg">
<IMG src="https://www.unix.com/images/2531_4703925.jpg"><BR><IMG src="https://www.unix.com/images/2531_7990660.jpg"><BR></P>

If you don't show us a representative sample of your input when you start, it should not be a surprise if input in a different format doesn't work with the solutions we suggest. Smilie
# 5  
Old 07-06-2013
Thanks Don, but even in your output I don't see it correctly Smilie

Code:
<IMG src="https://www.unix.com/images/\SERVER\DATA\Image\1948_4703925.jpg">

It misses multiple paths... argh... I thought we were so close Smilie

Last edited by Scrutinizer; 07-06-2013 at 04:34 AM.. Reason: Added code tags
# 6  
Old 07-06-2013
Sorry, I had a typo. Try:
Code:
sed '/^<[Ii][Mm][Gg]/sXsrc="[^"]*[\\/]Xsrc="https://www.unix.com/images/Xg' file

producing the output:
Code:
<IMG src="https://www.unix.com/images/3251_8832613.jpg">
<IMG src="https://www.unix.com/images/1303_4703925.jpg">
<IMG src="https://www.unix.com/images/1948_4703925.jpg">
<img src="https://www.unix.com/images/2721_7990660.jpg">
<IMG style="HEIGHT: 3.5in" src="https://www.unix.com/images/90417_4302013100410.jpg">
<IMG style="HEIGHT: 3.5in" src="https://www.unix.com/images/90417_4302013100411.jpg">
<IMG style="WIDTH: 434px; HEIGHT: 369px" height=433 src="https://www.unix.com/images/2360_7990660.jpg" width=438>
<IMG style="WIDTH: 3.5in" src="https://www.unix.com/images/5942_7990660.jpg">
<IMG src="https://www.unix.com/images/2531_4703925.jpg"><BR><IMG src="https://www.unix.com/images/2531_7990660.jpg"><BR></P>

# 7  
Old 07-06-2013
Hmm, I can't get latest regex to trigger at all on my data Smilie

Code:
$ sed '/^<[Ii][Mm][Gg]/sXsrc="[!"]*[\\/]Xsrc="https://www.unix.com/images/Xg' input > output
$ diff input output
$

tried with your latest one:

Code:
$ sed '/^<[Ii][Mm][Gg]/sXsrc="[^"]*[\\/]Xsrc="https://www.unix.com/images/Xg' input > output
$ diff input output
$

first script worked and had replaced, however this one for some reason does not eve trigger. I checked for the entries and yes, I do have them:

Code:
egrep "3251_8832613.jpg" input -c
5

here is few returns from the input file:
Code:
<IMG src="/3251_8832613.jpg">
<IMG style="HEIGHT: 3.5in" src="R:\Image\3251_8832613.jpg">

---------- Post updated at 09:29 PM ---------- Previous update was at 09:26 PM ----------

arghh, it was the ^ as my input file has a a lot more text before and ater, testing now data integrity Smilie

knock knock!
Moderator's Comments:
Mod Comment PLEASE use CODE tags!

Last edited by Don Cragun; 07-06-2013 at 02:25 AM.. Reason: Add CODE tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing a string

Hi All, I have a many folders in a directory under which there are many subdirectories containing text files containing the word "shyam" in them.I want all the files in all the directories containing "shyam to "ram" ?? sed "s/shyam/ram/g" does it ??But anyone can help me with the script ?? ... (3 Replies)
Discussion started by: Pradeep_1990
3 Replies

2. Programming

Need help for replacing a string in a text file at runtime !

Hi All, I am facing an issue... I need to replace some string in a text file while the same file is read by some other user at the same time. The other user is using it in the Read only mode. So I can't create a temporary file and write the content first and then write it back into the original... (2 Replies)
Discussion started by: agupta2
2 Replies

3. Shell Programming and Scripting

Help with replacing string

Hi All, I have below requirement: I need to read each line in file.txt and replace string starting from position 9 to 24 {111111111111111,222222222222222,333333333333333} by common string "444444444444444" and save file. File.txt: 03000003111111111111111 ... (3 Replies)
Discussion started by: smalode
3 Replies

4. UNIX for Dummies Questions & Answers

replacing a string with another string in a txt file

Dear all, I have a file like below. I want to replace all the '.' in the 3rd column with 'NA'. I don't know how to do that. Anyone has an iead? Thanks a lot! 8 70003200 21.6206 9 70005700 17.5064 10 70002200 . 11 70005100 19.1001 17 70008000 16.1970 32 70012400 26.3465 33... (9 Replies)
Discussion started by: forevertl
9 Replies

5. Shell Programming and Scripting

Help replacing string

Help! I'm trying this command but keep getting illegal syntax etc. awk '{ sub(/00012345/,"000123456"); print}' >newfile I don't understand. It works on one unix machine but not another! (4 Replies)
Discussion started by: Grueben
4 Replies

6. Shell Programming and Scripting

replacing a string in multiple subdirs to a new string??

I have following set of dirs: /dir1/dir2/subdir1 file1 file2 /dir1/dir3/subdir1 file4 file5 /dir1/dir4/subdir1 file6 file7 All of these files have a common string in them say "STRING1", How can I... (3 Replies)
Discussion started by: Hangman2
3 Replies

7. UNIX for Dummies Questions & Answers

Replacing string

Hi there, I'd like to replace STRING_ZERO in FILE_ZERO.txt with the value of VALUEi-th by using something like that: VALUE1=1000 VALUE2=2000 VALUE3=3000 for((i=1;i<=3;i++)); do sed "s/STRING_ZERO/$VALUE'$i'/" FILE_ZERO.txt >> FILE_NEW.txt; done but it doesn't work... Any help... (9 Replies)
Discussion started by: Giordano Bruno
9 Replies

8. Shell Programming and Scripting

Replacing Text in Text file

Hi Guys, I am needing some help writing a shell script to replace the following in a text file /opt/was/apps/was61 with some other path eg /usr/blan/blah/blah. I know that i can do it using sed or perl but just having difficulty writing the escape characters for it All Help... (3 Replies)
Discussion started by: cgilchrist
3 Replies

9. Shell Programming and Scripting

string replacing

hii, i need a unix command which replaces all occurrences of a substring within a string with another substring. My solution: string="plalstalplal" sub1="al" sub2="mlkl" echo sed 's/$s2/$s3/g' < s1 > p i want to know how to read the variables s2 and s3.. thaks a lot bye (1 Reply)
Discussion started by: priya_9patil
1 Replies

10. Shell Programming and Scripting

Replacing text

I was using the following code to replace the path names and it works when it is echo "$PWD/$f" | sed -e 's/^.*chris\.domain\.com/chris.domain.com/' IN fact it works great However I tried to incorporate a variable echo "$PWD/$f" | sed -e... (3 Replies)
Discussion started by: chrchcol
3 Replies
Login or Register to Ask a Question