Operating System - HP-UX
1753868 Members
7364 Online
108809 Solutions
New Discussion юеВ

Re: Strings replacement help needed

 
SOLVED
Go to solution
Md.Shahabuddin
Advisor

Strings replacement help needed

Hi,

I have to replace a string in a file of 1000 lines. That string is distributed and i want to replace only 200 lines out of 1000 lines of that particular strings.

It should be in interactive mode where i can get the information before replacing that string.

Thanks in advance.
Waiting for the earliset response.
10 REPLIES 10
James R. Ferguson
Acclaimed Contributor
Solution

Re: Strings replacement help needed

Hi:

Try this:

# perl -pe 'if ($n<200) {$n++ if s/XX/YY/g}' file

Change "XX" and "YY" to the patterns you need.

Regards!

...JRF...
OldSchool
Honored Contributor

Re: Strings replacement help needed

I believe the perl above is going to change the first 200 occurrances, while the request seems to be for 200 out of 1000, and he need to inspect it before commiting the change.

in this case I'd use vi.

to search:
/ where is the old text
then "n" to locate the next occurance

method of replacement is going to vary depending on if its a 'word', or exactly the same length or buried in some longer string...

insuffficient information for a complete answer.
James R. Ferguson
Acclaimed Contributor

Re: Strings replacement help needed

Hi (again):

The Perl script I suggested replaces up to 200-lines regardless of how many matches occur per line.

It is trivial to modify the substitution to "words" or simply any string sequence as I used.

I agree that more information would lead to a more complete answer.

...JRF...

Md.Shahabuddin
Advisor

Re: Strings replacement help needed


James.....

Thank you so much. Its working.
OldSchool
Honored Contributor

Re: Strings replacement help needed

JRF: I read this "It should be in interactive mode where i can get the information before replacing that string."

as "I need to see it in context before confirming change"...which apparently isn't the case
Md.Shahabuddin
Advisor

Re: Strings replacement help needed

U are right James.

can it be done in interactive mode?
Do u have any alternatives?
James R. Ferguson
Acclaimed Contributor

Re: Strings replacement help needed

Hi (again):

No, I made no provisions for doing this "interactively". I find it hard to imagine why you want to replace 200 of 1000 lines "interactively", which to me suggests confimation as you proceed. If this is true, do you count a line toward the "200" if you reject its change or only if you accept it?

Since I left the input file un-modified, I felt that this was an adequate "interactive" preview step in and of itself. Re-running the script after inspecting the results would then only require a simple redirection to a new output file or the change of the Perl swithes from '-pe' to '-pi -e' to affect an in-place update.

I interpreted your original question to mean _lines_ on which substitutions occur regardless of how many _times_ per line a substitution was made. How would that factor into your count toward "200" if you were to ask for confirmation at each step?

What is the problem you really want to solve? If it isn't an on-going one, using 'vi' as Oldschool suggested might be the fastest solution.

Regards!

...JRF...
Arturo Galbiati
Esteemed Contributor

Re: Strings replacement help needed

Hi,
I have to use VI and a lot of patience.
Find the string (/) and replace it (r), find next occurrence (n) and replace it (.) and so on.

HTH,
Art
Dennis Handly
Acclaimed Contributor

Re: Strings replacement help needed

>Art: Find the string (/) and replace it (r), find next occurrence (n) and replace it (.)

You can also use:
:first,last s/string1/string2/gc

The "c" asks you to confirm for each line.