【问题标题】:Bash script to split log file every string occurrence, and report inside the file specified lineBash脚本在每个字符串出现时拆分日志文件,并在文件指定行内报告
【发布时间】:2017-02-21 17:58:14
【问题描述】:

我有一个日志文件,准确地说它是类似于日志文件的自定义应用程序报告。我想创建一个 bash 脚本来管理这个日志,并在这个脚本启动时从这个单个文件生成“n”个文件。 目标是在出现“字符串”时“拆分”此日志文件,并捕获此字符串之后的单词,并忽略该行中的所有其余部分,以将此单词用作文件名的一部分。

我看到了一些基本脚本来拆分每个单词或字符串出现的日志文件,但它没有任何机会以其他方法保存或操作“内容”,它只是一个“拆分器”并以文件名 1.txt 保存,2.txt 等等。

为了了解上下文,我们正在讨论为“计算”软件许可证而生成的报告,该报告类似于:

blabla - Copyright (c) 1989-2016 Some Cool software without decent report. All Rights Reserved.
Cool Software License status on Wed 10/12/2016 03:02

License server status: port@serverhostname
    License file(s) on serverhostname: C:\Path\To\File\lic.txt:

  serverhostname: license server v0.7b

Users of service1:  (Total of 182 licenses issued;  Total of 0 licenses in use)

Users of service2:  (Total of 182 licenses issued;  Total of 180 licenses in use)

  "service2" v61.8, vendor: VENDORNAME
  floating license

    USER123 PC1234 PC1234 (v61.7) (SERVER/2501 4605), start Wed 10/12 14:56, 10 licenses
    USER311 PC3331 PC3331 (v61.2) (SERVER/2501 10621), start Mon 10/10 17:55, 12 licenses

Users of service3:  (Total of 182 licenses issued;  Total of 0 licenses in use)

Users of another_services:  (Total of 182 licenses issued;  Total of 0 licenses in use)

  "another_services" v61.8, vendor: ABAQUSLM
  floating license

    USER0021 PCNAME1 PCNAME1 (v61.4) (SERVER/2501 6852), start Wed 10/12 14:44
    USER0034 PCNAME03 PCNAME03 (v61.1) (SERVER/2501 13016), start Wed 10/12 10:51
    USER0123 PCNAME5 PCNAME4 (v61.4) (SERVER/2501 6852), start Wed 10/12 14:44

Users of plugin01:  (Total of 30 licenses issued;  Total of 0 licenses in use)

我想做的是为每个服务/插件创建一个单独的文本文件,所以我需要“拆分”,搜索/识别字符串,或者更好地以以下行开头:

用户

后跟 service_name,我需要将其存储在一个变量中以用作文件名的一部分。

在文本文件中,我只想报告包含 USER 和 PC 信息的字符串,跳过重复服务名称的两个描述行,以及有关服务的一些其他信息。

该文件是一个动态报告,所以在执行脚本之前,我无法真正知道数量或服务,或者“用户”行下的用户数量。此外,某些服务不能包含任何行。如果服务没有任何行,则可以生成没有任何行的文本文件。

非常感谢任何建议,因为我对 shell 脚本很陌生。

干杯!

【问题讨论】:

    标签: string bash shell parsing


    【解决方案1】:

    用 awk 试试这个:

    % awk '/^Users of/{name=$3} name{print > name}' file
    % ls -1 *service*
    

    您的要求并未全部完成,但请随时改进,我已经完成了更大的部分

    【讨论】:

    • 这对我的脚本来说是一个了不起的开始。我在终端内尝试命令并完成几乎所有的事情。不幸的是,我需要为每个生成的文件做一些字符串替换并跳过一些行,我们如何替换文件的“直接”打印,将信息存储在“变量”中,做一些事情,然后保存它?谢谢
    猜你喜欢
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2020-11-17
    • 2019-12-13
    • 1970-01-01
    • 2016-08-15
    相关资源
    最近更新 更多