|
|
|
| Title : |
Sed, a coder's best friend. This lovely linux command allows you to swap strings in a file. Great for changing a variable name in a script with
multiple files. |
| Categories : |
HTML and PHP, HTML and PHP |
 Tim Crider |
| Date : |
Jun 21st 1999 |
| Grade : |
1 of 5 (graded 1 times) |
| Viewed : |
5510 |
| File : |
No file for this code example. |
| Images : |
No Images for this code example. |
|
| Search : |
More code by Tim Crider |
|
| Action : |
Grade This Code Example
|
|
| Tools : |
My Examples List |
|
|
|
|
|
|
This is mainly for the Linux / *nix type developers. This will show you a small example of how to change the values of something in multiple files, without having to open each ove of
them up in an editor and doing it by hand.
example:
2 files: a.php3 b.php3
Contents
a.php3:
----
Hello World
----
b.php3:
----
GoodBye World
----
in that same directory type (Each on a new line ):
for i in *.php3 ; do
sed s^World^Planet^g < $i > $i.new
mv $i.new $i
done
Now read the files... they should read Hello Planet and Goodbye Planet
For a break down of those commands:
1 for i in *.php3 ; do
2 sed s^World^Planet^g < $i > $i.new
3 mv $i.new $i
4 done
Line 1:
sets up a loop that will execute the following functions until the construct no longer exists.
i is the counter, and *.php3 signifies that only files that end in .php3 will be effected.
; do actually starts the loop
Line 2:
sed (Stream EDitor), This command takes the target files, and reads them line by line... and changes the lines if they match the pattern.
syntax:
sed s^Target_String^Replacement_String^g < $i > $i.new
sed s = search and replace
Target_String = The string sed looks for
Replacement_String = The string that is put in place of the Target String
g = Globally. This means that if Hello Hello is on one line BOTH will be changed, if no g is added, only the first instance will be changed.
< $i = Takes the contents of the current file ($i) -- (Which is whatever *.php3 file is currently in the loop), and dumps it into set. (ex: x.php3)
> $i.new = Takes the RESULT of the sed command and dumps it into (current_file.new) (ex: x.php3.new)
Line 3:
moves x.php3.new to x.php3 (writes the changes to the old file)
Line 4:
ends the script.
This script has helped me incredibly time and time again, it has saved me hours of coding, by manipulating large groups of files without me ever having to open them up and editing
them. For more information on sed check out man sed on your linux box
Tim
|
|
| PHP3: Formmail. Just a cgi formmail, but than in PHP. It is easy to use! Categories : HTML and PHP, Email, PHP, Perl, HTML and PHP | | | A simple class with some HTML output functions that would come in handy for consistent page layout etc. Categories : PHP, PHP Classes, HTML and PHP, HTML, Navigation | | | Calendar using Date function Categories : HTML and PHP, PHP, Date Time, Calendar | | | news delete create dynamic Categories : HTML, HTML and PHP | | | phpCards - PHP/mySQL postcard script with web based admin to add, edit, and delete cards and categories. Very easy to install. Categories : PHP, Complete Programs, HTML and PHP, MySQL | | | Select with current month Categories : PHP, HTML and PHP, Date Time, Arrays | | | PHP4 session helper HTML file.
Categories : PHP, Java Script, HTML and PHP, Sessions | | | html split bar used to split in multiple pages a database result Categories : HTML and PHP, Databases, MySQL, PHP | | | Smart Counter - This little script is a plain and simple hit counter that uses cookies to determine whether or not the visitor has already been counted. Categories : Cookies, HTML and PHP, PHP | | | PageRank Display Categories : Search Engines, HTML and PHP, PHP | | | PHP, simple, counter, bala Categories : HTML and PHP, PHP, PHP Options and Info | | | Automatically printing the contents of an sql table in MySQL. Categories : MySQL, PHP, HTML and PHP, Databases | | | Dynamic Calender in PHP, Javascript and HTML. Categories : PHP, Java Script, HTML and PHP, Calendar | | | phpWebCam v1.0- Webcam management software - Automatically checks if you're online, and comes with a caption tool capable of handling multiple users. Categories : PHP, Complete Programs, HTML and PHP | | | function textwrap will wrap text to any desired width using <BR>\n as the default line break.
Default wrap width is 80 columns.
Categories : Strings, HTML and PHP, PHP | |
|
|
|