使用grep覆盖日志文件

时间:2017-06-30 10:03:54

标签: bash unix grep

您好我想问一下如何使用相同的文件覆盖complete.log但获取某些字符。

当前日志文件:

CompleteJob:test1.sas:
CompleteJob:test2.sas:

新日志文件:

test1.sas
test2.sas

我使用了grep和>>这样做;

grep "CompleteJob" complete.log | cut -f2 -d: >> complete.log

但我的结果如下:

CompleteJob:test1.sas:
CompleteJob:test2.sas:
test1.sas
test2.sas

4 个答案:

答案 0 :(得分:1)

sed  -i -r 's/^.+:(.*):.*/\1/g'  file

cat file
test1.sas
test2.sas

答案 1 :(得分:0)

最佳方法:

sed -i 's/^[^:]*:\([^:]*\):.*/\1/' complete.log
  • [^:]* - 匹配除:
  • 之外的任何字符

答案 2 :(得分:0)

使用GNU awk(gawk),您可以:

gawk -F: -i inplace '{print $2}' complete.log

答案 3 :(得分:0)

尝试另外一个可以在任何版本的awk中使用的选项。

awk -F":" '{print $2}' Input_file  > temp_file && mv temp_file  Input_file

将字段分隔符设置为冒号(:)并将Input_file的第二列打印到输出名为temp_file的文件。然后使用mv(move / rename)命令将temp_file重命名为Input_file。请注意这里&&表示如果第一个命令成功,则只应将temp_file重命名为Input_file。

相关问题