473,399 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Replacing a string

Hello All,

I have two lines as follows in my file.
<start>start of program<end>
<start>string to be replaced<end>

These lines are in the middle. There are blank spaces before the promt <. The sentence 'string to be replaced' need to be replaced by the sentence 'changed'.
The sentences 'string to be replaced is not always the same.

So i would like to delete the line 'string to be replaced and add my new sentence 'changed'.

I have tried the code,

Expand|Select|Wrap|Line Numbers
  1. while (<IN>){
  2. my $file = $_;
  3. if ($file =~ /\start of program/){
  4. print OUT $file;
  5. $file=<IN>;
  6. }
  7. $file =~ s/             /changed/g;
  8. }
As i told earlier the two sentences are in the middle with blank spaces before it, How could i incorporate it and print the first line as it and then change my second line to 'changed'.

Ramesh
Sep 17 '08 #1
5 1607
eWish
971 Expert 512MB
Is the data you provided exactly as it is in the file?

--Kevin
Sep 17 '08 #2
Hi Kevin,

The exact data in my file is

<main>x02_a08_s091<main>
<sub>Iteration problem<sub>

In my file, only the numbers change in my first line and the name of the problem in this case is Iteration, but the title changes every time. I need to change the heading to 'Actual problem' from 'Iteration problem'. My intention is to change the heading to 'Actual problem' irrespective of the heading mentioned initially. There are blank spaces before main and sub.
Sep 17 '08 #3
nithinpes
410 Expert 256MB
Hi Kevin,

The exact data in my file is

<main>x02_a08_s091<main>
<sub>Iteration problem<sub>

In my file, only the numbers change in my first line and the name of the problem in this case is Iteration, but the title changes every time. I need to change the heading to 'Actual problem' from 'Iteration problem'. My intention is to change the heading to 'Actual problem' irrespective of the heading mentioned initially. There are blank spaces before main and sub.

I am assuming you may want to retain those spaces.
Expand|Select|Wrap|Line Numbers
  1. while (<IN>){
  2. my $file = $_;
  3. if ($file =~ /x02_a08_s091/) { ## make this generic if pattern is same across
  4. print OUT $file;
  5. $file=<IN>;
  6. $file =~ s/(<sub>\s*)\S+/$1Actual/;
  7. print OUT $file;
  8. }
  9. }
  10.  
  11.  
Sep 18 '08 #4
Thanks for the hint. I just modified a little bit and it is working well

Expand|Select|Wrap|Line Numbers
  1. $file =~ s/(<sub>)\s+(\w+)/$1Actual/;
  2.  
I have one more query,In the above problem,I print the first line and modify the second line.If in a file, if I have to modify the file at 4 places, Is it possible to do it under one while loop or do I need to open the file 4 times and use the while loop each time?
Sep 21 '08 #5
nithinpes
410 Expert 256MB
Thanks for the hint. I just modified a little bit and it is working well

Expand|Select|Wrap|Line Numbers
  1. $file =~ s/(<sub>)\s+(\w+)/$1Actual/;
  2.  
I have one more query,In the above problem,I print the first line and modify the second line.If in a file, if I have to modify the file at 4 places, Is it possible to do it under one while loop or do I need to open the file 4 times and use the while loop each time?
The single while loop will iterate through the entire file. You can do the modification any number of times in the file, as long as the pattern is matched.
Sep 22 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: C# Learner | last post by:
What's the "standard" way of replacing a character in a string? Obviously, I can't say "myString = character;" because strings are immutable... So what would be the "standard" way of doing this?...
7
by: VMI | last post by:
If I have the string "Héllo", how can I replace char (é) with an 'e'? I cannot use the String.Replace() fuction. It has to be by replacing one char with another. Thanks.
7
by: Ryan Taylor | last post by:
Hi. I have some code that dynamically generates a PDF and spits this content directly to the web browser. I use HTMLDoc to create the Pdf's from html. So the user can click on a button "Print...
12
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: ...
1
by: WT | last post by:
Hello, I was using hastable to store collection of objects of the same class MyClass. I start replacing this hastable by a Dictionary<string,MyClass>....but I was storing this hastable in cache...
2
by: Alain | last post by:
Hi, I am working on a project where I need to convert international characters with acii values. Like André -> andre and Björn -> bjorn. How can I do this without replacing every single...
32
by: FireHead | last post by:
Hello C World & Fanatics I am trying replace fgets and provide a equavivalant function of BufferedInputReader::readLine. I am calling this readLine function as get_Stream. In the line 4 where...
0
by: Lakhi | last post by:
hi frnds, I need small help in String replacement I need to replace the text using replceAll() with Case-Insensitve . Is there any regular expression for this? i have this expression ...
2
by: gsuns82 | last post by:
Hi all, I have to replace accented characters from a input string with normal plain text.I have coded as follows. String input = "ÄÀÁÂÃ"; input=...
7
by: DarthBob88 | last post by:
I have to go through a file and replace any occurrences of a given string with the desired string, like replacing "bug" with "feature". This is made more complicated by the fact that I have to do...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.