Replace a string within 2 points and save it


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replace a string within 2 points and save it
# 1  
Old 08-03-2010
Replace a string within 2 points and save it

I've got this xml file

Code:
<file1>
some text here
</file1>

<file2>
some text here
</file2>

How do I change the text in element file1 to a sentence that I want, defined by variable $sentence. using ksh here.

Last edited by vgersh99; 08-03-2010 at 04:36 PM.. Reason: fixed code tags
# 2  
Old 08-04-2010
Try this,

Code:
mark=0;
sentence="Replace the text"

while read line
do
        if [ "$line" == "<file1>" ] ;then
                echo $line
                mark=1;
        elif [ "$line" == "</file1>" ] ;then
                echo $line
                mark=0;
        elif [ $mark -eq 1 ] ;then
                echo $sentence
        else
                echo $line
        fi


done < "file"

# 3  
Old 08-04-2010
Another approach:
Code:
awk -v s="New line" '
/<file1>/{print $0 RS s;f=1}
/<\/file1>/{f=0}
!f' file

 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace content from a file and save

Hi, Right now there is a file called 'qm.ini' which is owned by mqm:mqm and I am trying to replace a line from this file with something else and save. I am using the below perl command to replace and save within a shell script with a different user called 'mqadm' which is also part of mqm... (1 Reply)
Discussion started by: bdpl
1 Replies

2. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

3. Shell Programming and Scripting

perl : replace multiline text between two marker points

Hi there I just wondered if someone could give me some perl advice I have a bunch of text files used for a wiki that have common headings such as ---++ Title blah ---++ Summary blah ---++ Details Here is the multiline block of text I wish to (6 Replies)
Discussion started by: rethink
6 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Vi - replace words with points (.)

Hello guys, I'm trying to replace the word "i.e." for "ie." in Vi but everytime I used the search tool for start looking for it (this is: /i.e.), it finds every word that contains the "i" and "e" word. I tried the following command: :%s/i.e./ie./g However, it doesn't work. Any help... (2 Replies)
Discussion started by: Gery
2 Replies

5. Shell Programming and Scripting

After eliminating then save as a string

Hello, I am using HP-UX B.11.23 U ia64 2591592275 unlimited-user license I am trying to write a sh script on my own system to pass string of word as one parameter The format of the string will be the same But the content after : will be changed each time If you manage to have this as $*... (7 Replies)
Discussion started by: fahad.m
7 Replies

6. Shell Programming and Scripting

search a string in a line and save it in a variable

Hi I want to read a file line by line and search for a particular string in each line(say for example string containing @ )and save that string into a variable. Can someone suggest me the way to implement it.I am using K- shell Thanks Ishita (5 Replies)
Discussion started by: Ishita
5 Replies

7. Shell Programming and Scripting

replace character in a string pattern and save the change in same file

I am facing one problem, can any one please suggest me the command for the same in unix. I am using Ksh. I have a large file with the data that looks like below. "ROTO2-2007f","_US01","9/15/2007","9/21/2007",346492,"NICK, LCD WATCH"97,1,"NAPOLITJ ","BERGER,M Z & CO INC",0.01, ... (2 Replies)
Discussion started by: mihir0011
2 Replies

8. UNIX and Linux Applications

Gnuplot question: how to plot 3D points as colored points in map view?

I have a simple gnuplot question. I have a set of points (list of x,y,z values; irregularly spaced, i.e. no grid) that I want to plot. I want the plot to look like this: - points in map view (no 3D view) - color of each point should depend on its z-value. - I want to define my own color scale -... (0 Replies)
Discussion started by: karman
0 Replies

9. Shell Programming and Scripting

Search and replace string between 2 points

I have a file that contains the following string. connect odemgr/bank123@odsd I am liiking to write a scrupt that will change the par of the string between the "/" and the "@" anyhelp qwould be greatly appriciated. (3 Replies)
Discussion started by: whited05
3 Replies
Login or Register to Ask a Question