substitution without editing

I have a script with 100's of lines in it. I want to edit the script with out manually openning it (with vi editor or some thing like that). Basically I want to do a substitution using regular expression (%s/G_/28_/i). Please let me know the best way to do this.

What is your input and what is your expected output?

At the 86th line of the script (say: test.pl) I need to substitute the file name 'file1.txt' to file2.txt.

so basically my input will be the script and what I need to edit and out put will be the same script but with the edition (substitution) already done.

You could take the filename as a parameter for the script easily enough.

...
my $file=shift;
...
./myscript.pl filename.txt

or you could just

sed 's/file1\.txt/file2.txt/' < myscript.pl > output
cat output > myscript.pl

Every time you edit it like this you run the risk of making a mistake though, so a parameter would be much better. Never need to edit the file at all.

If I'm reading it correctly, that sample editing command will subsitute 28_ for the first instance of G_ or g_ in every line in a file. The following pipeline will accomplish the same thing non-interactively:

printf '%s\n' '1,$s/[gG]_/28_/' w q | ed -s file

Or, if you prefer, using a heredoc:

ed -s file <<'EOED'
1,$s/[gG]_/28_/
w
q
EOED

Regards,
Alister