This is a most tricky one for programmers. I got stuck with a situation. In a folder lot of php files start with
SED for Rescue
Find and Replace with SED
What is SED? SED is a stream editor or line editor in Linux or Unix. It reads input line by line (sequentially), applying the operation which has been specified via the command line (or a sed script), and then outputs the line. It was developed from 1973 to 1974 as a Unix utility by Lee E. McMahon of Bell Labs,[1] and is available today for most operating systems.
It isn’t really a true text editor or text processor. Sed is typically used for extracting part of a file using pattern matching or substituting multiple occurrences of a string within a file.
$ sed -i 's/foo/bar/g' /home/linux/people/programme.txt
This replaces all foo with bar.
Magic with SED and Our Find Command
$ find /home/dear/people -type f -exec sed -i 's/foo/bar/g' {} ;
There is a small shell script for this thing
#!/bin/bash for fl in *.php; do mv $fl $fl.old sed 's/FINDSTRING/REPLACESTRING/g' $fl.old > $fl rm -f $fl.old done
just replace the “*.php“, “FINDSTRING” and “REPLACESTRING” make it executable and you are set.
You can find a Documentation on http://www.brunolinux.com/02-The_Terminal/Find_and%20Replace_with_Sed.html
and Find command documentation on http://www.brunolinux.com/02-The_Terminal/The_Find_Command.html
CODE |
# perl -e "s/old_string/new_string/g;" -pi.save $(find DirectoryName -type f) |
Related articles
- How to use the find command in Linux (simplehelp.net)
- How to get applications via their port numbers (baibhavsingh.wordpress.com)
- You’ve Been Hacked! (famousbloggers.net)
- Thread class for multithreading in PHP (motane.lu)
- How do I increase upload file limit from 2MB to 10MB under Apache 2 UNIX / Linux web server? (pankajkhalkar.wordpress.com)
- What Is UNIX? (askwithamitkumar.wordpress.com)
- Could UNIX can run without the desktop (wiki.answers.com)
- Installing PHP driver for mongoDB (shakthydoss.wordpress.com)
- Aaron Toponce: Why Unix Is Superior (pthree.org)
- How to Override PHP Configuration Options (blogs.sitepoint.com)
- 18 Free Text Editors To Clean Up Your Code (blogs.sitepoint.com)
