cancel
Showing results for 
Search instead for 
Did you mean: 

modify string into file

SOLVED
Go to solution
Tonatiuh
Super Advisor

modify string into file

Red Hat Enterprise Linux 3

How can I modify certain string which is into a file? (using just a command line statament).
5 REPLIES
Ivan Ferreira
Honored Contributor
Solution

Re: modify string into file

You can do it to another file using sed. Like this:

sed s/search_string/replacement_string file

sed s/hello/goodbye myfile > myfile2

You can then rename or move the new file.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Gopi Sekar
Honored Contributor

Re: modify string into file


use sed or awk command scripts.

you can also do string replacement using perl scripts.

Regards,
Gopi
Never Never Never Giveup
Muthukumar_5
Honored Contributor

Re: modify string into file

You can use perl -pi -e 's///g' . It will automatically update into the same file without any string.

# Example:

file1:

hi
bye

# perl -pi -e 's/hi/first/g' file1

file1:

first
bye

hth.
Easy to suggest when don't know about the problem!
Guru Dutta
Frequent Advisor

Re: modify string into file

Just an short explanation to Muthukumars reply
perl -pi -e 's///g' .

-p -> assumes the following loop around your script. while (<>) {
# your script goes here
} continue {
print or die "-p destination: $!\n";
}

-i -> specifies that files processed by the <> construct are to be edited in-place

-e -> Specifies a line of script.

More details are in the man page.Hope it helps.
Ivan Ferreira
Honored Contributor

Re: modify string into file

This may be more difficult but works too, use a script wiht vi.

Create the script called script.vi:

^[:%s/string/replacement_string/g
^[:wq

The ^[: is created pressing CTRL+v and ENTER.

Then run

vi -s script.vi file_to_modify

The modifications will be done directly in the file.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?