【问题标题】:Extracting words from a log file从日志文件中提取单词
【发布时间】:2018-06-16 11:55:11
【问题描述】:

我正在尝试从日志文件中提取作业 ID,但在 bash 中提取它们时遇到问题。我尝试过使用 sed。

这是我的日志文件的样子:

> 2018-06-16 02:39:39,331 INFO  org.apache.flink.client.cli.CliFrontend 
> - Running 'list' command.
> 2018-06-16 02:39:39,641 INFO  org.apache.flink.runtime.rest.RestClient                      
> - Rest client endpoint started.
> 2018-06-16 02:39:39,741 INFO  org.apache.flink.client.cli.CliFrontend                       
> - Waiting for response...
>  Waiting for response...
> 2018-06-16 02:39:39,953 INFO  org.apache.flink.client.cli.CliFrontend                       
> - Successfully retrieved list of jobs
> ------------------ Running/Restarting Jobs -------------------
> 15.06.2018 18:49:44 : 1280dfd7b1de4c74cacf9515f371844b : jETTY HTTP Server -> servlet with content decompress -> pull from
> collections -> CSV to Avro encode -> Kafka publish (RUNNING)
> 16.06.2018 02:37:07 : aa7a691fa6c3f1ad619b6c0c4425ba1e : jETTY HTTP Server -> servlet with content decompress -> pull from
> collections -> CSV to Avro encode ->  Kafka publish (RUNNING)
> --------------------------------------------------------------
> 2018-06-16 02:39:39,956 INFO  org.apache.flink.runtime.rest.RestClient                      
> - Shutting down rest endpoint.
> 2018-06-16 02:39:39,957 INFO  org.apache.flink.runtime.rest.RestClient                      
> - Rest endpoint shutdown complete.

我正在使用以下代码提取包含 jobId 的行:

extractRestResponse=`cat logFile.txt`
echo "extractRestResponse: "$extractRestResponse

w1="------------------ Running/Restarting Jobs -------------------"
w2="--------------------------------------------------------------"
extractRunningJobs="sed -e 's/.*'"$w1"'\(.*\)'"$w2"'.*/\1/' <<< $extractRestResponse"
runningJobs=`eval $extractRunningJobs`
echo "running jobs :"$runningJobs

但是这并没有给我任何结果。我还注意到,当我打印 extractRestResponse 变量时,所有换行符都丢失了。

我也尝试使用此命令,但它没有给我任何结果:

extractRestResponse="sed -n '/"$w1"/,/"$w2"/{//!p}' logFile.txt"

【问题讨论】:

  • “我注意到所有换行符都丢失了”,您需要引用您的 "$extractRestResponse" 变量(很可能)。祝你好运。

标签: bash awk sed


【解决方案1】:

使用 sed:

sed -n '/^-* Running\/Restarting Jobs -*/,/^--*/{//!p;}' logFile.txt

说明:

  • 应用命令后,输入行默认回显到标准输出。 -n 标志禁止这种行为
  • /^-* Running\/Restarting Jobs -*/,/^--*/:匹配从^-* Running\/Restarting Jobs -*^--*(含)的行
  • //!p;:打印除与地址匹配的行

【讨论】:

    【解决方案2】:

    awk 来救援!

    awk '/^-+$/{f=0} f; /^-+ Running\/Restarting Jobs -+$/{f=1}' logfile
    

    【讨论】:

      【解决方案3】:

      你可以改进你原来的替换:

      sed -e 's/.*'"$w1"'\(.*\)'"$w2"'.*/\1/' <<< $extractRestResponse
      

      使用@ 作为分隔符:

      sed -n "s@.*$w1\(.*\)$w2.*@\1@p" <<< $extractRestResponse
      

      输出是$w1$w2之间的文本:

      > 15.06.2018 18:49:44 : 1280dfd7b1de4c74cacf9515f371844b : jETTY HTTP Server -> servlet with content decompress -> pull from > collections -> CSV to Avro encode -> Kafka publish (RUNNING) > 16.06.2018 02:37:07 : aa7a691fa6c3f1ad619b6c0c4425ba1e : jETTY HTTP Server -> servlet with content decompress -> pull from > collections -> CSV to Avro encode -> Kafka publish (RUNNING) >
      

      【讨论】:

        猜你喜欢
        • 2021-10-12
        • 2019-06-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-13
        • 1970-01-01
        相关资源
        最近更新 更多