I am looking for a replacing a string in a in multiple *.sql files in directory with a new string without using a temporary file
Normally I can use sed command as below
for W in ls `FILE*.sql`
do
sed 's/OLD/NEW/g' $W > TEMPFILE.dat
mv TEMPFILE.dat $W
done
But Here in my scenario I do not want to make use of a temporary file ,rather I would like to directly replace the string in the original files itself.
To keep the forums high quality for all users, please take the time to format your posts correctly.
First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags
```text
and
```
by hand.)
Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.
Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.
Thanks Panyam.It works great.
Could you please explein me how the above command works.
I am new to perl
Thanks
Raghu
---------- Post updated at 06:51 AM ---------- Previous update was at 06:45 AM ----------
Thanks to vgersh99 for that example:
printf '1,$ s/OLD/NEW/g\nw! infile\nq!'| ex - infile
[/quote]
Thanks Vgersh.That works great.
Can you please explain me the above code in detail.I have never used such command to find replace a string in a file before
Here 'perl' executed with assumption that it will be an input loop - like 'read file until end-of-file'.
That is done by the option -p. Else that option means to print out each processed line, and that makes it write it back
However, it would not work out without the option -i - 'in-place editing', as without that the perl would print it on screen.
After all, there is the main option: -e - tells to perl to execute a command, instead of looking for a file with commands.
So, perl has an execution command and the input loop file and is directed to process it into the same file.
printf '1,$ s/OLD/NEW/g\nw! infile\nq!'| ex - infile
Here the 'ex' editor is editing the 'infile' and writes result into the 'infile', mentioned after 'w!' in 'printf ...'
The printf is produces the set of the 'ex' commands: '1,$' - address range - from 1st line to the last one; the substitute command itself; \n - to make another command: w! <file> - so, write to the file <file>; again, new command - q! - quit.
So produced printf output is piped to 'ex'.
The ' - ' is to suppress ex output, or to run silently.