Replace date in files 1


Scenario

You have many simple text log files of a system, where the date is formatted using the slash character / and you want to update the dates to some other date.
Usually when using the sed, the slash character is reserved for separating the parts of the expression you want to evaluate.
In this case though, we can go around this limitation by using another symbol as the separator, leaving the slash character available for us to use in our regular expression.

Example

The following example demonstrates just that. You will see that we used the colon character : in the place of the separator allowing us to use the slash character / in the expression.

sed -i 's:2015/01/06:2015/01/15:g' *.log

What we did here was change the character that sed uses to delimit its options with :, this way we could use / as any other character.
All log files in that folder will get automatically updated since we used *.log at file selection parameter.
The -i parameter instructs sed to make all replacements in place. i.e. All files will be modified to reflect the changes, it will not create new ones.


Leave a Reply

One thought on “Replace date in files

  • George Michael

    This command can be used to replace in place paths in files.

    For example:
    sed -i ‘s:/opt/OSELAS.Toolchain-2011.11.1/arm-cortexa9-linux-gnueabi/gcc-4.6.2-glibc-2.14.1-binutils-2.21.1a-kernel-2.6.39-sanitized/sysroot-arm-cortexa9-linux-gnueabi/:/opt/OSELAS.Toolchain-2012.12.1/arm-cortexa9-linux-gnueabihf/gcc-4.7.3-glibc-2.16.0-binutils-2.22-kernel-3.0.35-sanitized/sysroot-arm-cortexa9-linux-gnueabihf/:g’ configure README;

    This command will replace in place all instances of:
    /opt/OSELAS.Toolchain-2011.11.1/arm-cortexa9-linux-gnueabi/gcc-4.6.2-glibc-2.14.1-binutils-2.21.1a-kernel-2.6.39-sanitized/sysroot-arm-cortexa9-linux-gnueabi/
    with:
    /opt/OSELAS.Toolchain-2012.12.1/arm-cortexa9-linux-gnueabihf/gcc-4.7.3-glibc-2.16.0-binutils-2.22-kernel-3.0.35-sanitized/sysroot-arm-cortexa9-linux-gnueabihf/
    in the files ‘configure; and ‘README’.