Click to See Complete Forum and Search --> : Need a script to search ascii files and replace text
movEAX_444
05-04-2007, 11:10 PM
I have thousands of ascii files which I want to search for specific strings and have those strings replaced, or simply deleted.
I could use grep to search. I haven't scripted in ages.. I recall maybe sed or awk would be used to edit the files?
This is an emergency and I can't risk messing up the files, otherwise I would find some reference manuals and be on my way. Just a quick overview or pseudo code would be sufficient. Thanks!
happybunny
05-05-2007, 12:40 AM
EDIT: TEST AND BACKUP AND TEST: I take no respondibility for what this might do!
well, for a lack of anyone else responding:
sed -ui s/oldword/newword/g *
The -ui switch seems to vary from version to version, but you need the switch that will replace and save the file...without it, it will just show you what it wants to do and not really re-write the file.
Test first of course
bwkaz
05-06-2007, 02:05 PM
GNU sed supports the -i option, which tells it to operate on the file in place. With any other version of sed, the -i option may not be supported (it's not any kind of standard, I don't think), as sed is a stream editor, not a file editor. ;)
The -u option tells sed to operate in "unbuffered mode", which does more reads from and writes to the file. It's not required to make -i work. :)
But yes -- if sed reads a line which doesn't contain the match pattern, it writes it out (by default) with no changes. So if all your files are in one directory, you can just run them all through sed once. (If not, you'll have to set up some kind of "find <stuff> -exec sed <stuff> \;" type command.)