Use sed to replace last character after matching a line

If you are a coder like me we tend to discover after we have made a script that we need to change something on multiple lines.

Recently I have a script whereby added a “log_message” function which would echo a phrase.

However, I wanted to append a double parenthesis after the last character in the entire file.

For example.

Inside of myscript.sh I have the following

# grep Beginning myscript.sh
        log_message "Beginning function enableOrDisableRealstore"
        log_message "Beginning function modulePowerCycleChassis"
        log_message "Beginning function checkFipsLicense"
        log_message "Beginning function enableFips"
        log_message "Beginning function backupFips"
        log_message "FIPS: Beginning function isFipsEnabled"
        log_message "Beginning function isEncryptionEnabled"
        log_message "Beginning function isControllerPrimary"
        log_message "Beginning function isPeerControllerUpAndExecuting"
        log_message "Beginning function preZeroizeControllersValidation"
        log_message "Beginning function preZeroizeHaValidation"
        log_message "Beginning function preZeroizeDataCollection"
        log_message "Beginning function validateMachine"
        log_message "Beginning function cleanupDatabases"
        log_message "Beginning function isTxosRunning"
        log_message "Beginning function cleanupTunables"
        log_message "Beginning function checkControllerFailLogs"
        log_message "Beginning function getNetworkInfo"
        log_message "Beginning function getMDRaidInfo"
        log_message "Beginning function eraseSsdZion"
        log_message "Beginning function secureEraseSsd"
        log_message "Beginning function _retry_secerase for $disk"
        log_message "Beginning function _powercycle_disk_slot for $disk"
        log_message "Beginning function check_dd_process"
        log_message "Beginning function writeZeroesToHdd"
        log_message "Beginning function verifyBootDomPartition"
        log_message "Beginning function listDOM"
        log_message "Beginning function makePartitionsOnSSD"
        log_message "Beginning function formatRealstore"
        log_message "Beginning function updateDiskSignature"
        log_message "Beginning function deleteLogsAndCores"
        log_message "Beginning function resetRootPassword_default"
        log_message "Beginning function resetRootPassword_internal"
        log_message "Beginning function resetRootPassword"
        log_message "Beginning function resetAdminPasswordToSerialNumber"
        log_message "Beginning function validateSerialNumber"

My goal was to replace the last quote with ()” so that each line would look like

log_message "Beginning function validateSerialNumber()"

This is easily resolved using sed!

# sed -i.bak 's/\(.*Beginning.*\)[^ ]$/\1\(\)\"/' myscript.sh

the above backs up myscript.sh to myscript.sh.bak

Then it replaces the last quote with ()”

The result is

# grep Beginn myscript.sh
        log_message "Beginning function enableOrDisableRealstore()"
        log_message "Beginning function modulePowerCycleChassis()"
        log_message "Beginning function checkFipsLicense()"
        log_message "Beginning function enableFips()"
        log_message "Beginning function backupFips()"
        log_message "FIPS: Beginning function isFipsEnabled()"
        log_message "Beginning function isEncryptionEnabled()"
        log_message "Beginning function isControllerPrimary()"
        log_message "Beginning function isPeerControllerUpAndExecuting()"
        log_message "Beginning function preZeroizeControllersValidation()"
        log_message "Beginning function preZeroizeHaValidation()"
        log_message "Beginning function preZeroizeDataCollection()"
        log_message "Beginning function validateMachine()"
        log_message "Beginning function cleanupDatabases()"
        log_message "Beginning function isTxosRunning()"
        log_message "Beginning function cleanupTunables()"
        log_message "Beginning function checkControllerFailLogs()"
        log_message "Beginning function getNetworkInfo()"
        log_message "Beginning function getMDRaidInfo()"
        log_message "Beginning function eraseSsdZion()"
        log_message "Beginning function secureEraseSsd()"
        log_message "Beginning function _retry_secerase for $disk()"
        log_message "Beginning function _powercycle_disk_slot for $disk()"
        log_message "Beginning function check_dd_process()"
        log_message "Beginning function writeZeroesToHdd()"
        log_message "Beginning function verifyBootDomPartition()"
        log_message "Beginning function listDOM()"
        log_message "Beginning function makePartitionsOnSSD()"
        log_message "Beginning function formatRealstore()"
        log_message "Beginning function updateDiskSignature()"
        log_message "Beginning function deleteLogsAndCores()"
        log_message "Beginning function resetRootPassword_default()"
        log_message "Beginning function resetRootPassword_internal()"
        log_message "Beginning function resetRootPassword()"
        log_message "Beginning function resetAdminPasswordToSerialNumber()"
        log_message "Beginning function validateSerialNumber()"
This entry was posted in Command Line FU and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *