Search code examples
bashunixsedtee

Shell Script : How to use Tee command with sed command


Before:

main 2>&1 | tee -a $log_file;

This is working fine but it is throwing stderr in $log_file as shown below. I want to replace:

"ERROR (version 5.3.0.0-213, build 1 from 2015-02-02_12-17-08 by buildguy) : (stderr)"

With:

"NOTE (version 5.3.0.0-213, build 1 from 2015-02-02_12-17-08 by buildguy) : (stderr)"

I want the version and date to be in regex format.

After:

main 2>&1 | tee -a | sed -i 's|ERROR (version 5.3.0.0-213, build 1 from 2015-02-02_12-17-08 by buildguy) : (stderr)|NOTE (version 5.3.0.0-213, build 1 from 2015-02-02_12-17-08 by buildguy) : (stderr)|g' $log_file;

Error which are coming at downloading time

Few lines coming at the end(The lines are jumbled)


Solution

  • Could you try this;

    main 2>&1 | sed "/ERROR.*[0-9]\{1\}.[0-9]\{1\}.[0-9]\{1\}.[0-9]\{1\}-[0-9]\{3\}.*20[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\}_[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\}.*(stderr)/ s/ERROR/INFO/" | tee -a $log_file
    

    To find version, the regex;

    [0-9]\{1\}.[0-9]\{1\}.[0-9]\{1\}.[0-9]\{1\}-[0-9]\{3\}  :
    

    To find date, regex : for example (2015-02-02_12-17-08)

    20[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\}_[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\}
    

    if date is like 2015-12-03 11.37.25, you should use

    20[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\} [0-9]\{2\}.[0-9]\{2\}.[0-9]\{2\}