Hi
I need to write a script that read a input file that had same statement repeatedly to replace only 2nd & 5th time repeated statements (ex: This is UNIX forum) with another statement ( UNIX forum threads in Shell programming) with out modifying 1st,3,4th repeated statements. I am planning to do with sed. Any other available better approaches?
With no clue as to what your data looks like, I shall assume that yours is the simplest case where the string to be replaced occurs all by itself in a line.
Thus -
$
$ cat -n f3
1 This is line 1. Some stuff here.
2 This is UNIX. And some more stuff here. The weather's good.
3 This is UNIX forum.
4 Fourth line here.
5 This is UNIX forum.
6 And some more.
7 This is UNIX forum.
8 The line that does not have the target string.
9 This is UNIX forum.
10 Some filler stuff here.
11 And finally -
12 This is UNIX forum.
13 The end.
$
$
$ ##
$ perl -lne 'BEGIN {$from="This is UNIX forum.";
> $to="==> UNIX forum threads in Shell programming <=="}
> if (/$from/) {
> $i++;
> if ($i == 2 or $i == 5){print $to} else {print}
> } else {print}' f3
This is line 1. Some stuff here.
This is UNIX. And some more stuff here. The weather's good.
This is UNIX forum.
Fourth line here.
==> UNIX forum threads in Shell programming <==
And some more.
This is UNIX forum.
The line that does not have the target string.
This is UNIX forum.
Some filler stuff here.
And finally -
==> UNIX forum threads in Shell programming <==
The end.
$
$
tyler_durden
Thanks Tyler..Got it