Window's 2000 Global Find and Replace Solution using awk 
Author Message
 Window's 2000 Global Find and Replace Solution using awk

Greetings

Thanks you all for the help.  The following lines will perform a global find
and replace under Windows 2000 using the Windows "for", "findstr", "del" and
"rename" commands and "awk" in a batch file.

findstr /m /c:%1 %3 > temp-.txt
for /F %%f in (temp-.txt) do awk '{ gsub(%1,%2); print }' %%f > temp-%%f
for /F %%f in (temp-.txt) do del %%f
for /F %%f in (temp-.txt) do rename temp-%%f %%f
del temp-.txt

The %1, %2 and %3 are command line substitution variables which is the way
Windows 2000 deals with command line arguments.

Line 1 is the Windows equivalent of grep. The /m prints only the file name
if a file contains a match. The /c: option uses the specified text as a
literal search string. The > redirects the output thus creating a text file
"temp-.txt" which contains the list of files in which the specified text was
found.

Line 2 uses the Windows for command to invoke awk and create new files with
the replaced text.

Line 3 uses the for command to delete the old files.

Line 4 renames the new files to the old file names

Line 5 deletes the temp-.txt file.

Thus if the batch file gfar.bat (Global Find And Replace.bat) contains the
above lines of code you would enter the global find and replace command at
the command prompt as follows.

gfar "string to replace" "new string" file(s)

For example to replace the word Big with the word Small in all text files
that contain the word Big you would enter the gfar.bat command as follows:

gfar "Big" "Small" *.txt

Cheers
Steven



Tue, 10 Aug 2004 12:01:40 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. can't find tcom package on Windows 2000 Server and NT

2. can't find tcom package on Windows 2000 Server and NT

3. windows.h not found, compiling 1.6.7 under cygwin/windows 2000

4. Global Find and Replace?

5. MatriDigm's Year 2000 Solution

6. Cobol year 2000 solutions by TOPIC 2000

7. awk on windows 2000

8. Awk for Windows 2000?

9. system('dir') and Windows 2000

10. Replacing text using awk (but in a c shell)

11. Using ScanSoft Developer's Kit 2000 with C5

12. WOW32.DLL error when using CW5.0 EE in Windows 2000

 

 
Powered by phpBB® Forum Software