简体   繁体   English

Shell脚本:如何在sed命令中使用Tee命令

[英]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. 这样做工作正常,但是将stderr放入$ log_file中,如下所示。 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) 很少有行结尾(行混杂)

You should precise your need, it's pretty hard to read your code by now. 您应该调整您的需求,目前很难阅读您的代码。

There is two option here : 这里有两个选择:

  • You get the mainstream and alternate him before saving into your log file 您会成为主流,并在保存到日志文件之前替换他
  • You format your log file at the end 您在末尾格式化日志文件

First option 第一选择

I can't test it, however, it should be like this : 我无法测试,但是应该是这样的:

main 2>&1 | SED COMMAND | tee -a $log_file

Second option 第二选择

Tested, it works. 经过测试,可以正常工作。

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

Sed will edit the file specified inline because of -i option. 由于-i选项,Sed将编辑内联指定的文件。

REGEX 正则表达式

If you want to change all ERROR by NOTE , then, you should just use this sed command 如果要通过NOTE更改所有错误 ,则应仅使用此sed命令

sed -i "s/ERROR/NOTE/g" $log_file;

And if you want to be more specific, take a look at this answer : using SED with wildcard 如果您想更具体一点,请看以下答案: 将SED与通配符一起使用

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) 要查找日期,请使用正则表达式:例如(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 如果日期如2015-12-03 11.37.25,则应使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM