Index: trunk/BNC/scripts/Common.pm
===================================================================
--- trunk/BNC/scripts/Common.pm	(revision 10512)
+++ trunk/BNC/scripts/Common.pm	(revision 10513)
@@ -15,4 +15,5 @@
 use File::Basename;
 use File::Spec::Functions;
+use Fcntl ':flock';
 use Proc::ProcessTable;
 
@@ -228,4 +229,61 @@
 }
 
+#** @function replace ($fileName, $pattern, $replacement)
+# @brief  Search and replace a string in a file
+# @param  $fileName    [required] file with full path
+# @param  $pattern     [required] Search pattern string
+# @param  $replacement [required] Replacement string
+# @return true on success, false on error or patter not found
+#*
+sub replace {
+    my ( $fileName, $pattern, $replacement ) = @_;
+
+    # TODO getOptions();
+
+    TRACE "Search and replace string \'$pattern\' in file $fileName";
+
+    my $fileName_tmp = "${fileName}.tmp";
+    my ( $OLD, $NEW );
+    if ( !open ( $OLD, "<", $fileName ) ) {
+        ERROR("Could not open file [$fileName]: $!");
+        return 0;
+    }
+    flock ( $OLD, LOCK_SH ) || ERROR "Could not lock file '$fileName'";    # lock shared (others can read)
+
+    if ( !open ( $NEW, ">", $fileName_tmp ) ) {
+        ERROR("Could not write file [$fileName_tmp]: $!");
+        flock ( $OLD, LOCK_UN ) || ERROR "Could not unlock file '$fileName'";
+        close ($OLD)            || ERROR "Could not close file $fileName: $!";
+        return 0;
+    }
+
+    my $numOfReplacements = 0;
+    while (<$OLD>) {
+        if ( $_ =~ /$pattern/ ) {
+            $_ =~ s/$pattern/$replacement/;
+            $numOfReplacements++;
+        }
+        print $NEW $_;
+    }
+    flock ( $OLD, LOCK_UN ) || ERROR "Could not unlock file '$fileName'";
+    close ($OLD) || ( ERROR "Could not close file $fileName: $!"     && return 0 );
+    close ($NEW) || ( ERROR "Could not close file $fileName_tmp: $!" && return 0 );
+
+    unless ($numOfReplacements) {
+        WARN "Pattern not found in file";
+        unlink ("$fileName_tmp");
+        return 0;    # ist das ein Fehler?
+    }
+    else {
+        TRACE "$numOfReplacements pattern replaced";
+        rename ( $fileName,     "${fileName}.orig" ) || ( ERROR "Could not rename file $fileName: $!" && return 0 );
+        rename ( $fileName_tmp, $fileName )
+          || ( ERROR "Could not rename file $fileName_tmp to $fileName: $!" && return 0 );
+        unlink ("${fileName}.orig") || ERROR "Could not remove file ${fileName}.orig";
+        return 1;
+    }
+
+}
+
 #** @function isAlreadyRunning ()
 # @brief   Check if this program is already running.
