If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

The 'find' command will output every filename that matches '*' (i.e. EVERY file) starting from the current directory ('.')

The 'for' loop will obviously cause the nested statements to occur for the list of substituted filenames.

The 'sed' command will substitute the word you require. Note: the initial occurrence of the word 'word' is not necessary but it makes the command more efficient as sed will only look to perform the substitution on lines that contain the match (as opposed to every line by default).

The use of '&&' is important because it means that the following command (i.e. 'mv') will only execute if the 'sed' command executed successfully.

Re: Is it possible to replace word in every file?

The commands from the other two are very powerful, possibly too powerful. sed will textually replace any occurrence of a consecutive string of characters with the new ones, even if the old characters are part of a larger word e.g. 'the' is a substring of 'these' and sed s'/the/tea/' will output 'tease' rather than an unchanged 'the'.

perl is good for this: it has delimiters for word boundaries and the appropriate command is s/\bthe\b/tea/;

Before making the substitutions, it is worth testing the hypothesis that there are no substrings.

Originally posted by msetjadi
HI All,

I am very new in unix and i am not familiar if my requirements can be done in unix.

I need to replace a common word that exists inside thousands file,is impossible to edit one by one.

To all expert, are there any unix command that i can use to do this.
It might not be possible to do it once probably i need to creat a script but is it possible?

Originally posted by Damian Ibbotson
(the above doesn't take into the 'word' being at the beginning of the line or the end of the line, so you'd have to string a few commands together to get exactly what you want!)

The 'find' command will output every filename that matches '*' (i.e. EVERY file) starting from the current directory ('.')

The 'for' loop will obviously cause the nested statements to occur for the list of substituted filenames.

The 'sed' command will substitute the word you require. Note: the initial occurrence of the word 'word' is not necessary but it makes the command more efficient as sed will only look to perform the substitution on lines that contain the match (as opposed to every line by default).

The use of '&&' is important because it means that the following command (i.e. 'mv') will only execute if the 'sed' command executed successfully.

I will be very obliged if you could help me with this query.I have used the code you have shared on this forum to replace one single word in all my files but I get an error. I am not sure why I am getting this error. Whether this is a ubuntu specific problem or a problem with my script.

The 'find' command will output every filename that matches '*' (i.e. EVERY file) starting from the current directory ('.')

The 'for' loop will obviously cause the nested statements to occur for the list of substituted filenames.

The 'sed' command will substitute the word you require. Note: the initial occurrence of the word 'word' is not necessary but it makes the command more efficient as sed will only look to perform the substitution on lines that contain the match (as opposed to every line by default).

The use of '&&' is important because it means that the following command (i.e. 'mv') will only execute if the 'sed' command executed successfully.