Replace the strings in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace the strings in file
# 1  
Old 06-15-2012
Replace the strings in file

Hello members,

I been following this forums since very long time.
I need to do one job.
In my script I am evaluating one variable, lets say n=100.

Now i have xml file inside which i need to replace the numbers in the desired lines with the evaluated number(n) +1.

For example let's say my file has the following entries.


Code:
 <EventMappingRule alarmFieldID="200" type="TeMIPEnumeration" alarmFieldName="Specific Problem"/>
          <Constant label="asiOutAvailableBitrate" type="Enum">1094</Constant>
        </TeMIPEnumMappingRule>
        <AdditionalTextMappingRule>
 <TeMIPEnumMappingRule type="Constant">
          <EventMappingRule alarmFieldID="200" type="TeMIPEnumeration" alarmFieldName="Specific Problem"/>
          <Constant label="softwareMgmtCompletion" type="Enum">1098</Constant>
        </TeMIPEnumMappingRule>
        <AdditionalTextMappingRule>

Desired file

Code:
<EventMappingRule alarmFieldID="200" type="TeMIPEnumeration" alarmFieldName="Specific Problem"/>
          <Constant label="asiOutAvailableBitrate" type="Enum">101</Constant>
        </TeMIPEnumMappingRule>
        <AdditionalTextMappingRule>
 <TeMIPEnumMappingRule type="Constant">
          <EventMappingRule alarmFieldID="200" type="TeMIPEnumeration" alarmFieldName="Specific Problem"/>
          <Constant label="softwareMgmtCompletion" type="Enum">102</Constant>
        </TeMIPEnumMappingRule>
        <AdditionalTextMappingRule>

Let me know how can i achieve this. Don't ask me to loop through whole file, since it is huge file.

I am thinking some thing like following:

Code:
cat $filename | sed 's/<match>/>match with replace>/g'


Thanks in advance
sai

Last edited by Scrutinizer; 06-16-2012 at 04:29 AM.. Reason: code tags
# 2  
Old 06-15-2012
Hi


Code:
$ awk -F'[>]' '/Constant label/{sub(/^[0-9]+/,++i,$2);}1' i=100 OFS='>' file
<EventMappingRule alarmFieldID="200" type="TeMIPEnumeration" alarmFieldName="Specific Problem"/>
<Constant label="asiOutAvailableBitrate" type="Enum">101</Constant>
</TeMIPEnumMappingRule>
<AdditionalTextMappingRule>
<TeMIPEnumMappingRule type="Constant">
<EventMappingRule alarmFieldID="200" type="TeMIPEnumeration" alarmFieldName="Specific Problem"/>
<Constant label="softwareMgmtCompletion" type="Enum">102</Constant>
</TeMIPEnumMappingRule>
<AdditionalTextMappingRule>

# 3  
Old 06-16-2012
One more condition

Hey thanks Smilie that worked but just wanted to add one more condition to it.

In the condition below, i want to check and substitute only lines which has "alarmFieldName="Specific Problem"/>" in their preceding lines.

Code:
awk -F'[>]' '/Constant label/{sub(/^[0-9]+/,++i,$2);}1' i=100 OFS='>' file


Last edited by Scrutinizer; 06-16-2012 at 04:29 AM.. Reason: code tags
# 4  
Old 06-16-2012
I see you already have a solution in AWK, just wanted to show a perl one liner as an example:
Code:
perl -le '$n=100;while (<>){chomp;s/\>\d+\</$n++/e;print $_;}' test.txt

# 5  
Old 06-17-2012
@spacebar

Hi,

The script I am using is a shell script. I don't want to invoke perl interpreter here.

In the condition below, i want to check and substitute only lines which has "alarmFieldName="Specific Problem"/>" in their preceding lines.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

How to replace the complex strings from a file using sed or awk?

Dear All, I am having a requirement to find the difference between 2 files and generate a discrepancy report out of it as an html page. I prefer using diff -y file1 file2 since it gives user friendly layout to know any discrepancy in the record and unique records among the 2 file. Here's how it... (12 Replies)
Discussion started by: Badhrish
12 Replies

2. Shell Programming and Scripting

Replace multiple strings of a file

Hi, I have an array variable "arr" that reads string from a file "vari.txt". Thus, the array will be of variable length depending how many entries are present in "vari.txt" I use a for loop to traverse through the array. vari.txt (in this sample we have 2 entries, but it can have more... (5 Replies)
Discussion started by: mohtashims
5 Replies

3. Shell Programming and Scripting

Replace the .txt file between two strings in XML file

Hi i am having XML file with many number of lines,I need to replace between two strings with .txt file using awk. For ex <PersonInfoShipTo ------------------------------ /> My requirement is to replace the content between <PersonInfoShipTo ------------------------------ /> help me. Thanks... (9 Replies)
Discussion started by: Padmanabhan
9 Replies

4. Shell Programming and Scripting

Replace Contents between 2 strings in a file with contens of another file

Please I want to replace all the contents beween "Section" and "Ensection" in file1 with all contents in file2. Example: file1: Section "Screen" DefaultDepth 24 SubSection "Display" Depth 8 ViewPort 0 0 Modes "1024x768" "800x600" "640x480" EndSubsection SubSection "Display" Depth... (9 Replies)
Discussion started by: powelltallen
9 Replies

5. UNIX for Advanced & Expert Users

Find and replace txt between two strings in flat file

Hi There... I need to serach and replace strngs in a text file. My file has; books.amazon='Let me read' news.bestseller='xyz' expected output is books.amazon=NONFOUND news.bestseller=NONFOUND Can I first find the text between string1= books.amazon=' and string2= ' (locate the text... (1 Reply)
Discussion started by: Hiano
1 Replies

6. Shell Programming and Scripting

Search replace strings between single quotes in a text file

Hi There... I need to serach and replace a strings in a text file. My file has; books.amazon='Let me read' and the output needed is books.amazon=NONFOUND pls if anybody know this can be done in script sed or awk.. i have a list of different strings to be repced by NONFOUND.... (7 Replies)
Discussion started by: Hiano
7 Replies

7. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

8. Shell Programming and Scripting

Replace multiple strings in a file.

Hello Freinds, Hope, you all are doing well. I have to replace the static strings from one file (File 1) with the dynamic strings from the another file (File2). I've written a shell script for this.Below is the contents. while read line do field=`echo $line | awk '{print $1}'` ... (9 Replies)
Discussion started by: singh.chandan18
9 Replies

9. Shell Programming and Scripting

Shell script to replace strings to and from a file

Hello All, I have 2 files 1 ) source file eg asasa 1.2.3.4 adfhsdfsdfasdf zxzxzx 2.3.4.56 dsadasdasdsadasd kjjkjkjk 30.3.4.5 asdsadsadsadsadsad vxcvxcvx 1.2.3.4 qwewqewqeqweqwe 2) patern file 1.2.3.4 A 2.3.4.56 B 30.3.4.5 C I need the source to be changed to asasa A... (2 Replies)
Discussion started by: nitinkgoud
2 Replies

10. Shell Programming and Scripting

replace strings in a file

Hi I want to change the following passwd: files nis group: files nis in /etc/nsswitch.conf to be passwd: files compat group: files compat I tried cp -p nsswitch.conf nsswitch.conf.old (3 Replies)
Discussion started by: melanie_pfefer
3 Replies
Login or Register to Ask a Question