Sponsored Content
Top Forums Programming find & Replace text using two non-unique delimiters. Post 303013819 by bedtime on Wednesday 28th of February 2018 06:53:55 PM
Old 02-28-2018
find & Replace text using two non-unique delimiters.

I can find and replace text when the delimiters are unique. What I cannot do is replace text using two NON-unique delimiters:

Ex.,

Code:
"This html code <text blah >contains <garbage blah blah >. All tags must go,<text > but some must be replaced with <garbage blah blah > without erasing other info."

Code:
delimiter1: '<garbage'
delimiter2: '>'
replace with: 'important info'

delimiter3: '<'
delimiter4: '>'
replace with: ''


I get this:

Code:
This html code contains important info


And I want this:

Code:
This html code contains important info. All tags must go, but some must be replaced with important info without erasing other info.


The issue is that the program keeps seeing the '>' which is tied in with the'<text >' tag and using it instead of using the '>' which is tied in with '<garbage'.

In my real-world scenario, these tags are much more complicated and will have a variety of text inbetween whilst being different sizes and having different endings; also, certain tags must be deleted first, second, and so on, so changing the order will not help this situation.

I want to make code that understands that the '>' delimiter, which I want to use as an end position for '<garbage' tag, can only be the one which comes closest AFTER the '<garbage' tag (and if it understands that, then it cannot make a mistake); but I do not know how to do this. I have it working perfectly in an awk program, but not in C++. And I will not use boost; I'd rather then just stick with awk in that case.

Here is my code:

Code:
// Compile and run with:
//
// g++ -O -Wall replace.cpp -o replace
//

#include<iostream>
#include<string>
#include<fstream>

using namespace std;

string replaceText (string text, string tStart, string tStop, string tReplace)
{

	long int begPos;
	long int endPos;
	int found=1;

	while ((text.find(tStart) != std::string::npos) && (found == 1)) {

		found = 0;

		begPos = text.find(tStart);
		endPos = text.find(tStop);

			if (tStop != "")
                        {
				text.replace(begPos, endPos - begPos + tStop.length(), tReplace);
				found = 1;
			}else{
				text.replace(begPos, tStart.length(), tReplace );
				found = 1;
			}

		// Used for testing to see positions of replaced text:
		std::cout << "Replacing from: " << tStart << " ...to... " << tStop << " at Start Pos: " << begPos << " Stop Pos: " << endPos << " with " << tReplace << " \n" << endl;

	}

	return text;
}

int main(int argc, char* argv[])
{
	
	keyFound="This html code <text blah >contains <garbage blah blah >. All tags must go, <text > but some must be replaced with <garbage blah blah > without easing other info.";

	// Run this code twice: once with the below line of code commented, and once without:
	keyFound=replaceText(keyFound, "<garbage", ">", "important info");
	keyFound=replaceText(keyFound, "<", ">", "");

	std::cout << keyFound << endl;

	return 0;
}


I am not expecting an entire answer, but maybe if someone could lead me to a resource which has a fitting answer. I've been looking all around, and I cannot seem to find anything. Also, I am new to C++.

I understand that this is an incredibly complicated thing with no simple answer.

Thank you.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find & Replace

I get a text file with 70+ columns (seperated by Tab) and about 10000 rows. The 58th Column is all numbers. But sometimes 58th columns has "/xxx=##" after the numeric data. I want to truncate this string using the script. Any Ideas...:confused: (3 Replies)
Discussion started by: gagansharma
3 Replies

2. Shell Programming and Scripting

find & incremental replace?

Looking for a way using sed/awk/perl to replace port numbers in a file with an incrementing number. The original file looks like... Host cmg-iqdrw3p4 LocalForward *:9043 localhost:9043 Host cmg-iqdro3p3a LocalForward *:10000 localhost:10000 Host cmg-iqdro3p3b LocalForward... (2 Replies)
Discussion started by: treadwm
2 Replies

3. Shell Programming and Scripting

get part of file with unique & non-unique string

I have an archive file that holds a batch of statements. I would like to be able to extract a certain statement based on the unique customer # (ie. 123456). The end for each statement is noted by "ENDSTM". I can find the line number for the beginning of the statement section with sed. ... (5 Replies)
Discussion started by: andrewsc
5 Replies

4. 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

5. Homework & Coursework Questions

[Scripting]Find & replace using user input then replacing text after

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: (o) Checkout an auto part: should prompt the user for the name of the auto part and borrower's name: Name:... (2 Replies)
Discussion started by: SlapnutsGT
2 Replies

6. Shell Programming and Scripting

Find and add/replace text in text files

Hi. I would like to have experts help on below action. I have text files in which page nubmers exists in form like PAGE : 1 PAGE : 2 PAGE : 3 and so on there is other text too. I would like to know is it possible to check the last occurance of Page... (6 Replies)
Discussion started by: lodhi1978
6 Replies

7. Red Hat

copy & replace text

how can i copy a certain word from a text file then use this word to replace in another text file?? i tried to use something like: awk '{print "Hit the",$1,"with your",$2}' /aaa/qqqq.txt > uu.txt but i can't add an argument to point to the second file which i will replace in. please... (8 Replies)
Discussion started by: mos33
8 Replies

8. UNIX for Dummies Questions & Answers

Find & Replace

Hi I am looking to rename the contents of this dir, each one with a new timestamp, interval of a second for each so it the existing format is on lhs and what I want is to rename each of these to what is on rhs..hopefully it nake sense CDR.20060505.150006.gb CDR.20121211.191500.gb... (3 Replies)
Discussion started by: rob171171
3 Replies

9. Shell Programming and Scripting

Finding a text in files & replacing it with unique strings

Hallo Everyone. I have to admit I'm shell scripting illiterate . I need to find certain strings in several text files and replace each of the string by unique & corresponding text. I prepared a csv file with 3 columns: <filename>;<old_pattern>;<new_pattern> ... (5 Replies)
Discussion started by: gordom
5 Replies

10. Shell Programming and Scripting

Delete characters & find unique IP addresses with port

Hi, I have a file having following content. <sip:9376507346@97.208.31.7:51088 <sip:9907472291@97.208.31.7:51208 <sip:8103742422@97.208.31.7:51024 <sip:9579892841@97.208.31.7:51080 <sip:9370904222@97.208.31.7:51104 <sip:9327665215@97.208.31.7:51104 <sip:9098364262@97.208.31.7:51024... (2 Replies)
Discussion started by: SunilB2011
2 Replies
All times are GMT -4. The time now is 10:06 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy