Replacing the domain name in a large file

I had a large text file (MySql database dump) with many instances of a domain name that I needed to change out for a website that’s being deployed under a new domain name. My first attempts to do a search & replace from a simple text editor failed and eventually crashed the editor. As well, it was not something I could repeat exactly the next time I had to migrate that same database again. The Linux utility ‘sed’ is a streaming editor that would easily handle large files and seemed like a simple solution. I ran it against the file twice, being careful to replace only the domain name as it appeared in URLs, and not where it appeared in any stored email addresses.

  • www.my-old-domain.com —> www.objectclarity.com/~newhome
  • ://my-old-domain.com —> ://objectclarity.com/~newhome

[code]sed -i.bak -e ‘s%www\.my-old-domain\.com%www.objectclarity.com/~newhome%g’ huge-mysql-dump.sql

sed -e ‘s%:\/\/my-old-domain\.com%://objectclarity.com/~newhome%g’ huge-mysql-dump.sql
[/code]

Here’s a breakdown of what the commands mean:

(-i.bak) creates a backup of the original file.
(-e) tells sed that a script follows
The part in single quotes is my search-and-replace expression in the following form:

‘s%regex%replacement%g’

(s) defines a search & replace expression
(%) is my delimiter. “/” is the default delimiter but as I’m working with URLs, I don’t have to escape each character and results in a simpler espression.
(g) tells sed to replace *all* instances of my regex with the replacement string.

Each command ran in about 1 second on a 40Mb file and it did the job perfectly.

What roles do I have?

Ever wanted to know what Oracle roles your oracle user has? This simple query will offer up a complete list including the objects and privileges for each as specified in the role.

[code]
SELECT a.role, a.owner || ‘.’ || a.table_name tbl,
a.column_name,
a.privilege,
a.grantable
FROM role_tab_privs a, dba_role_privs b
WHERE a.role = b.granted_role
AND b.grantee LIKE UPPER (USER)
[/code]

Obviously it requires that you have a catalog role that allows you access to:

  • role_tab_privs
  • dba_role_privs
  • Zend Framework Quickstart on Ubuntu 11.10

    Running through the Zend Framework quickstart on Ubuntu 11.10, I ran into a few issues. Granted they’re not huge issues, but they slowed me down nonetheless.

    1) Make sure that the php.ini for the cli is updated to include the zend framework. See the /etc/apache2/php.ini for the changes required.

    2) When creating the /data and /scripts folders, they need to be in the root of your quickstart app, beside your “public” folder.

    3) I didn’t have the Sqlite driver installed on my machine so that had to be installed before I could create the database.

    AN ERROR HAS OCCURED: The sqlite driver is not currently installed

    To fix this, I installed the “php-mdb2-driver-sqlite” package from Synaptic.

    After fixing these 3 problems, the data loading script worked like a charm!

    There was one more glitch getting the Quickstart app completed. Apparently in Ubuntu 11.10, Sqlite has been upgraded to Sqlite3 where Zend is still using the older version. Here’s a workaround to add the prior version to get through: https://bugs.launchpad.net/ubuntu/+source/php5/+bug/875262/comments/10

    Handy PL/SQL

    Every now and then I need to write a block of Oracle PL/SQL that’s a little more involved. I’ve had this block of script put aside as an example of how to include functions and procedures in an anonymous block.
    [code]
    — PLSQL code
    BEGIN
    DECLARE
    — Equivalent of a package level variable
    i INTEGER := 0;

    PROCEDURE doStuff (description IN VARCHAR2)
    IS
    i INTEGER;
    BEGIN
    DBMS_OUTPUT.put_line (‘description is ‘ || description);

    i := i + 1;
    END;

    FUNCTION calcStuff (i_value IN INTEGER)
    return integer
    iS
    i_dummy INTEGER;
    BEGIN
    i := i + 1;
    DBMS_OUTPUT.put_line (‘Function called ‘ || i || ‘ time(s)’ );

    return i_value + i;
    END;

    BEGIN
    — Call our shiny new procedure
    doStuff (‘one’);
    doStuff (‘two’);
    doStuff (‘three’);

    — Call our new function
    DBMS_OUTPUT.put_line (‘Function returned: ‘ || calcStuff(100));
    DBMS_OUTPUT.put_line (‘Function returned: ‘ || calcStuff(150));
    DBMS_OUTPUT.put_line (‘Function returned: ‘ || calcStuff(200));

    END;
    END;
    [/code]