Replace & sign to &amp word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace & sign to &amp word
# 1  
Old 11-23-2010
Replace & sign to &amp word

Hi, I have text file abc.txt. In this file, I have the following data.

Input:
Code:
Mr Smith &amp Mrs Smith
Mr Smith &apos Mrs Smith
Mr Smith & Mrs Smith
Mr Smith& Mrs Smith
Mr Smith &Mrs Smith

Output:
Code:
Mr Smith &amp Mrs Smith
Mr Smith &apos Mrs Smith
Mr Smith &amp Mrs Smith
Mr Smith&amp Mrs Smith
Mr Smith &ampMrs Smith

In line 3, 4, and 5. I want to change & sign to &amp word but do not want to change anything in line 1, and line 2. If I use

Code:
sed 's/&/&amp/g' abc.txt

command, it changes to &amp everywhere.

Thanks for your help

Naveed

Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 11-23-2010 at 12:03 PM.. Reason: code tags, please!
# 2  
Old 11-23-2010
Code:
sed 's/&\([^a-z]\)/\&amp\1/g' abc.txt

# 3  
Old 11-23-2010
Thanks a lot jlliagre. It works great.

Naveed
# 4  
Old 11-24-2010
Code:
sed '2,$s/&/&aa/'

# 5  
Old 11-24-2010
Huh ?
Your code give that output:
Code:
Mr Smith &amp Mrs Smith
Mr Smith &aaapos Mrs Smith
Mr Smith &aa Mrs Smith
Mr Smith&aa Mrs Smith
Mr Smith &aaMrs Smith

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I want replace string <enclosure>&#x22;</enclosure> with <enclosure>&#x5e;</enclosure> in xml

I have xml files with with extension .ktr in subfolders i want to replace the string <enclosure>&#x22;</enclosure> with <enclosure>&#x5e;</enclosure> i have written logic but it is not working correctly sed -i '' 's#<enclosure>&\#x22;</enclosure>#<enclosure>&\#x5e;</enclosure>#g' *.ktr ... (3 Replies)
Discussion started by: reddy12
3 Replies

2. Shell Programming and Scripting

SFTP Shell Script Get & Delete && Upload & Delete

Hi All, Do you have any sample script, - auto get file from SFTP remote server and delete file in remove server after downloaded. - only download specify filename - auto upload file from local to SFTP remote server and delete local folder file after uploaded - only upload specify filename ... (3 Replies)
Discussion started by: weesiong
3 Replies

3. Shell Programming and Scripting

Replace dashes positions 351-357 & 024-043 with 0 & replace " " if exis with 04 at position 381-382

I need to replace dashes (i.e. -) if present from positions 351-357 with zero (i.e. 0), I also need to replace dash (i.e “-“) if present between position 024-043 with zero (i.e. 0) & I replace " " (i.e. 2 space characters) if present at position 381-382 with "04". Total length of record is 413.... (11 Replies)
Discussion started by: lancesunny
11 Replies

4. Emergency UNIX and Linux Support

SSH Is hanging and the & sign does not seem to be working

I am having an issue where I am do an SSH to about 30 servers one at a time however my script is getting hung up sometimes on the SSH. I thought the & at the end as seen below would fire it and move on but that does not seem to be working. #!/bin/sh for remsys in trumpetsnail angel... (15 Replies)
Discussion started by: LRoberts
15 Replies

5. Shell Programming and Scripting

SSH Script is sticking, & sign not doing what I would expect

I am having an issue where I am do an SSH to about 30 servers one at a time however my script is getting hung up sometimes on the SSH. I thought the & at the end as seen below would fire it and move on but that does not seem to be working. #!/bin/s for remsys in trumpetsnail angel delphin... (3 Replies)
Discussion started by: LRoberts
3 Replies

6. Shell Programming and Scripting

Sort a the file & refine data column & row format

cat file1.txt field1 "user1": field2:"data-cde" field3:"data-pqr" field4:"data-mno" field1 "user1": field2:"data-dcb" field3:"data-mxz" field4:"data-zul" field1 "user2": field2:"data-cqz" field3:"data-xoq" field4:"data-pos" Now i need to have the date like below. i have just... (7 Replies)
Discussion started by: ckaramsetty
7 Replies

7. Shell Programming and Scripting

replace & with &amp; xml file

Hello All I have a xml file with many sets of records like this <mytag>mydata</mytag> <tag2>data&</tag2> also same file can be like this <mytag>mydata</mytag> <tag2>data&</tag2> <tag3>data2&amp;data3</tag3> Now i can grep & and replace with &amp; for whole file but it will replace all... (4 Replies)
Discussion started by: lokaish23
4 Replies

8. UNIX for Dummies Questions & Answers

How to compare 2 files & get specific value & replace it in other file.

Hiiii Friends I have 2 files with huge data. I want to compare this 2 files & if they hav same set of vales in specific rows & columns i need to get that value from one file & replace it in other. For example: I have few set data of both files here: a.dat: PDE-W 2009 12 16 5 29 11.11 ... (10 Replies)
Discussion started by: reva
10 Replies

9. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

10. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies
Login or Register to Ask a Question