使用awk格式化文件以获得YYYY-MM-DD,值格式。

huangapple go评论61阅读模式
英文:

formatting files with awk to get it to YYYY-MM-DD,value format

问题

我想要这样的内容:

日期,温度
2023-06-29,38
2023-06-29,38

这是我迄今为止的进展(仍在尝试中):

>> awk -F"[[:blank:]]{2,}" '{print FILENAME,$3}' /log/msg/sched/4G-Fan-Temperature-JZ/2023-06-2[89]*_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-28_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-28_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-28_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-28_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-28_14:20:02_eNB_618159_table.log temperature
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-28_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-28_14:20:02_eNB_618159_table.log 37
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-28_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-28_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-28_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-29_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-29_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-29_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-29_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-29_14:20:02_eNB_618159_table.log temperature
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-29_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-29_14:20:02_eNB_618159_table.log 38
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-29_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-29_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-29_14:20:02_eNB_618159_table.log
>>

这是我正在处理的文件的外观:

>> cat /log/msg/sched/4G-Fan-Temperature-JZ/2023-06-2[89]*_eNB_618159_table.log
[2023-06-28 14:20:02] NE=eNB_618159 , USER=t828226 , USER_IP=127.0.0.1 , OPERATION=get , RESULT=OK
NODE-PATH=samsung4g-access-enbdu/managed-element/hardware-management/digital-unit/fan
RPC-REPLY-TABLE=
----------------------------------
NE Name   NE ID        temperature
----------------------------------
ATIDF     eNB_618159   37
----------------------------------
;

[2023-06-29 14:20:02] NE=eNB_618159 , USER=t828226 , USER_IP=127.0.0.1 , OPERATION=get , RESULT=OK
NODE-PATH=samsung4g-access-enbdu/managed-element/hardware-management/digital-unit/fan
RPC-REPLY-TABLE=
----------------------------------
NE Name   NE ID        temperature
----------------------------------
ATIDF     eNB_618159   38
----------------------------------
;

这是我在此示例中使用的两个文件:

>> ls -l /log/msg/sched/4G-Fan-Temperature-JZ/2023-06-2[89]*_eNB_618159_table.log
-rw-rw---- 1 vsm aceman 380 Jun 28 14:20 /log/msg/sched/4G-Fan-Temperature-JZ/2023-06-28_14:20:02_eNB_618159_table.log
-rw-rw---- 1 vsm aceman 380 Jun 29 14:20 /log/msg/sched/4G-Fan-Temperature-JZ/2023-06-29_14:20:02_eNB_618159_table.log
英文:

I want something like this:

date,temperature
2023-06-29,38
2023-06-29,38

This is as far as I have got so far(still trying)

>>awk -F"[[:blank:]]{2,}" '{print FILENAME,$3}' /log/msg/sched/4G-Fan-Temperature-JZ/2023-06-2[89]*_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-28_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-28_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-28_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-28_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-28_14:20:02_eNB_618159_table.log temperature
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-28_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-28_14:20:02_eNB_618159_table.log 37
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-28_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-28_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-28_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-29_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-29_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-29_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-29_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-29_14:20:02_eNB_618159_table.log temperature
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-29_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-29_14:20:02_eNB_618159_table.log 38
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-29_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-29_14:20:02_eNB_618159_table.log
/log/msg/sched/4G-Fan-Temperature-JZ/2023-06-29_14:20:02_eNB_618159_table.log

>>

This is what the files look like that I am working with

 >>
    >> cat /log/msg/sched/4G-Fan-Temperature-JZ/2023-06-2[89]*_eNB_618159_table.log
    [2023-06-28 14:20:02] NE=eNB_618159 , USER=t828226 , USER_IP=127.0.0.1 , OPERATION=get , RESULT=OK
    NODE-PATH=samsung4g-access-enbdu/managed-element/hardware-management/digital-unit/fan
    RPC-REPLY-TABLE=
    ----------------------------------
    NE Name   NE ID        temperature
    ----------------------------------
    ATIDF     eNB_618159   37
    ----------------------------------
    ;
    
    [2023-06-29 14:20:02] NE=eNB_618159 , USER=t828226 , USER_IP=127.0.0.1 , OPERATION=get , RESULT=OK
    NODE-PATH=samsung4g-access-enbdu/managed-element/hardware-management/digital-unit/fan
    RPC-REPLY-TABLE=
    ----------------------------------
    NE Name   NE ID        temperature
    ----------------------------------
    ATIDF     eNB_618159   38
    ----------------------------------
    ;

These are the 2 files I am using in this example

>> ls -l /log/msg/sched/4G-Fan-Temperature-JZ/2023-06-2[89]*_eNB_618159_table.log
-rw-rw---- 1 vsm aceman 380 Jun 28 14:20 /log/msg/sched/4G-Fan-Temperature-JZ/2023-06-28_14:20:02_eNB_618159_table.log
-rw-rw---- 1 vsm aceman 380 Jun 29 14:20 /log/msg/sched/4G-Fan-Temperature-JZ/2023-06-29_14:20:02_eNB_618159_table.log

答案1

得分: 3

awk 'BEGIN{ print "日期,温度" } /RESULT=/{ date=substr($1,2) } /ATIDF/{ print date "," $3 }' 2023-06-2[89]*_eNB_618159_table.log 输出:

 日期,温度 2023-06-28,37 2023-06-29,38 

> substr($1,2): 输出从第二个字符到最后的 $1(这里去掉了前导 [

英文:

I assume that the leading spaces are not part of your files.

awk 'BEGIN{ print "date,temperature" }
     /RESULT=/{ date=substr($1,2) }
     /ATIDF/{ print date "," $3 }' 2023-06-2[89]*_eNB_618159_table.log

Output:
<pre>
date,temperature
2023-06-28,37
2023-06-29,38
</pre>

> substr($1,2): Output $1 from second character to last (here remove leading [)

huangapple
  • 本文由 发表于 2023年7月7日 06:34:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76632903.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定