|
|
|
| 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 : |
8830 |
| 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 | | | TreeView - Finally a working tree view function to be used as you want. Simple create the Table using the code provided and you will be able to have a tree view in your project. Download the zip to get the images. Categories : PHP, HTML and PHP, Navigation | | | Using PHP im HTML image tags Categories : PHP, HTML and PHP, Graphics, Beginner Guides | | | How to preset a text string in a textarea input field Categories : HTML, HTML and PHP, PHP, Beginner Guides | | | PHP mySQL learning example This is a complete program to modify tables
in a mySQL database on a nice and neat way. Categories : PHP, MySQL, Complete Programs, HTML and PHP | | | string justification align center text Categories : Strings, PHP, HTML and PHP | | | Random quote generator using MySQL and PHP. Allows HTML tags inside quotes and
features a marker so a sigle quote will not be repeated after consecutive page
loads.
Categories : PHP, HTML and PHP, MySQL | | | Function that allows a Javascript cookie to be set after HTML has been outputted to the page.
Categories : PHP, Java Script, Cookies, HTML and PHP | | | Simple PHP/3 Access Counter (using GD and DBM functions) Categories : Databases, PHP, Graphics, HTML and PHP, dBase | | | Complete, simple working example of login screen and check on a unique page using php functions, cookies and mysql database. Categories : PHP, Cookies, MySQL, HTML and PHP, Authentication | | | CachedFastTemplate class, extends CDI's FastTemplate to allow page caching Categories : HTML and PHP, HTML, PHP Classes | | | Pull Down Surfing - Surf on Change Categories : Java Script, MySQL, HTML and PHP, PHP, Databases | | | PHPixel - output of 1 pixel transparent/colored gif for counters etc. Categories : PHP, Graphics, HTML and PHP | | | Form validations using javascript and including all validations in one message Categories : HTML and PHP, Java Script, Form Processing | | | How to let a user download a picture by clicking on it instead of needing to right click and Save-As. Categories : HTTP, PHP, HTML and PHP, Filesystem | |
|
|