Find and Replace code help needed


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find and Replace code help needed
# 1  
Old 08-23-2005
Find and Replace code help needed

I have a file "dbshot.xml" that contains lines that need replacing in a batch format but the parameters are based on two lines.

Ex.
<role roletype="01">
<status>1

needs to be changed to

<role roletype="01">
<status>0

I can't use simply "<status>1" replace since the roletype in the preceding line must be considered a precondition of changing the status.

Any help is greatly appreciated. Smilie
# 2  
Old 08-23-2005
Code:
$ cat orgill.pl
#!/usr/bin/perl

open( FH, "./datafile" ) || die "Couldn't open file...\n";

while ( <FH> ) {
   $data .= $_;
}

$data =~ s/<role roletype="01">\n<status>1\n/<role roletype="01">\n<status>0\n/g
;

print $data;
$ ./orgill.pl > newfile

Cheers
ZB
# 3  
Old 08-24-2005
In ksh

Code:
$ cat dbshot.xml 
<role roletype="01">
<status>1

<role roletype="02">
<status>1

blah blah
<status>1

$ cat script.sh
sed '/roletype="01"/ {
n
s/<status>1/<status>0/
}' dbshot.xml

$ script.sh
<role roletype="01">
<status>0

<role roletype="02">
<status>1

blah blah
<status>1

bjorno

Last edited by bjorno; 08-24-2005 at 04:43 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and replace, perl code

Hi, Can somebody tell whats wrong with "find and replace perl code". I wanted to find "\n".write(status,data" and replace with "\n",data" in multipls files. #!/usr/bin/perl my @files = <*.c>; foreach $fileName (@files) { print "$fileName\n"; my $searchStr0 =... (4 Replies)
Discussion started by: chettyravi
4 Replies

2. Shell Programming and Scripting

Replace char between chars - help needed

Hello, I have a csv file with "^" as text delimiters and "|" as field delimiters. It's converted from a xls file. One record looks like this: ^Tablete Internet^|Archos|501838|^Tableta Internet ARCHOS 80 G9 ...| ... (more lines) ... "501|838"^|330.00|USD|sl|12|0|Link|^router wireless 150... (10 Replies)
Discussion started by: go0ogl3
10 Replies

3. Shell Programming and Scripting

sed replace after pattern help needed

Hello, I have a file with multiple lines like this: /film/4295/"_class="titre_article">50/50I would like to change all occurence of / after > with _ to have this: /film/4295/"_class="titre_article">50_50Thank you edit: This could also be change all / starting with the 4th occurrence... (2 Replies)
Discussion started by: patx
2 Replies

4. Shell Programming and Scripting

Search for a pattern and replace. Help needed

I have three variables $a, $b and $c $a = file_abc_123.txt $b = 123 $c = 100 I want to search if $b is present in $a. If it is present, then i want to replace that portion by $c. Here $b = 123 is present in "file_abc_123.txt", so i need the output as "file_abc_100.txt' How can this be... (3 Replies)
Discussion started by: irudayaraj
3 Replies

5. Shell Programming and Scripting

Urgent help needed !!!....to replace a exact string

Hi experts, As i am a novice unix player...so need help for the below query...banged my head from quite a while...:confused: i have a set of html files, in which i need to search for string "Page"(case sensitive) and then replace the same with some numeric code ,say, "XXX1234". Here in... (1 Reply)
Discussion started by: rahulfhp
1 Replies

6. UNIX for Advanced & Expert Users

Assistance Needed With Find/Replace in Vi

Hello All I have always had a question about find and replace in Vi. As this uses Vi, sed, and RegEx I never knew how or where to post the question but I thought I would give it a shot here. Say I have a text file filled with the following: Sue, your IP address is 192.168.1.10 which is... (4 Replies)
Discussion started by: NoSalt
4 Replies

7. Shell Programming and Scripting

Complex Search/Replace Multiple Files Script Needed

I have a rather complicated search and replace I need to do among several dozen files and over a hundred occurrences. My site is written in PHP and throughout the old code, you will find things like die("Operation Aborted due to....."); For my new design skins for the site, I need to get... (2 Replies)
Discussion started by: UCCCC
2 Replies

8. Shell Programming and Scripting

Help needed in replace

Hi, I have a pipe delimited file. I need to change the timestamp in the second field in all the records to a specific value. can you please let me know how i can do it? sample file with 2 reocrds. The time i need to change is in red below. Please let me know how I can change only... (7 Replies)
Discussion started by: dsravan
7 Replies

9. UNIX for Advanced & Expert Users

search a replace each line- help needed ASAP

can someone help me with the find and replace command. I have a input file which is in the below format: 0011200ALN00000000009EGYPT 000000000000199900000 0011200ALN00000000009EGYPT 000000000000199900000 0011200ALN00000000008EGYPT 000000000000199800000 0011200ALN00000000009EGYPT ... (20 Replies)
Discussion started by: bsandeep_80
20 Replies

10. Shell Programming and Scripting

awk: find and replace in certain field only, help needed

I got a sample file like this. $ cat test 12|13|100|s 12|13|100|s 100|13|100|s 12|13|100|s I want to replace all 100 by 2000 only in 3rd field using "awk" This is replacing all 100's :-( $ awk -F "|" '{gsub( /100/,"2000");print}' test 12|13|2000|s 12|13|2000|s 2000|13|2000|s... (5 Replies)
Discussion started by: jkl_jkl
5 Replies
Login or Register to Ask a Question