将多个标题信息字段附加到文件,直到找到下一个标题 [英] Append multiple header information fields to file until next header found

查看:156
本文介绍了将多个标题信息字段附加到文件,直到找到下一个标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用bash解析许多日志文件。日志文件如下所示:

I am attempting to parse many log files using bash. The log files look something like this:

"",1/8/2016
"Timezone",-6

"Serial No.","000001"
"Location:","LS_trap_2c"
"High temperature limit (�C)",-20
"Low temperature limit (�C)",-40
"Date - Time","Temperature (�C)"
"8/11/2015 12:00",28.0
"8/11/2015 14:00",28.5
"8/11/2015 16:00",24.0

"",1/8/2016
"Timezone",-6

"Serial No.","000002"
"Location:","LS_trap_2D"
"High temperature limit (�C)",-20
"Low temperature limit (�C)",-40
"Date - Time","Temperature (�C)"
"8/11/2015 12:00",28.0
"8/11/2015 14:00",28.5

我想附加序列号,和Location(或许还有其他人)到每一行,直到到达下一个标题,并将其输出到 master.csv 文件。该文件最终应该是这样:

I want to append the Serial no., and Location (and maybe others later on) onto each line until the next header is reached, and output this to a master.csv file. The file should look like this ultimately:

"",1/8/2016
"Timezone",-6

"Serial No.","000001"
"Location:","Trap_2c"
"High temperature limit (�C)",-20
"Low temperature limit (�C)",-40
"Date - Time","Temperature (�C)"
LS_trap_2c,000001,"8/11/2015 12:00",28.0
LS_trap_2c,000001,"8/11/2015 14:00",28.5
LS_trap_2c,000001,"8/11/2015 16:00",24.0

"",1/8/2016
"Timezone",-6

"Serial No.","00002"
"Location:","LS_trap_2D"
"High temperature limit (�C)",-20
"Low temperature limit (�C)",-40
"Date - Time","Temperature (�C)"
LS_trap_2D,00002,"8/11/2015 12:00",28.0
LS_trap_2D,00002,"8/11/2015 14:00",28.5

这里是一个问题,帮助我做类似的文件使用bash sed处理:

Here is a question that helped me do processing on similar files using bash sed:

Bash将文件信息附加到文件的每一行,直到找到下一个标题

这个oneliner非常适合查找

This oneliner is great for finding a header, storing it in holdspace and adding it into the front of each line

sed -r '/^"/h;//!{G;s/(.*)\n.*"(.*)"/\2,\1/}' fil.csv  >masfil.csv

这种方法没有用于将多个字符串附加到前面,因为我不确定如何使用sed的多个保留​​空间。此外,我不知道如果sed是最好的方法做到这一点。我不太熟悉sed,所以任何指针都会非常感激。

This approach has not worked for appending multiple strings to the front since I am unsure of how to use multiple hold spaces with sed. Also, I'm not sure if sed is the best way to do this. I'm not very familiar with sed so any pointers would be greatly appreciated.

推荐答案

awk 以拯救!

awk to the rescue!

假设您的数据一致

awk -F, '/"Serial No."/ {sn = $2} 
         /"Location:"/  {loc = $2} 
         /"([0-9]{1,2}\/){2}[0-9]{4} [0-9]{2}:[0-9]{2}"/ 
                        {$0 = loc FS sn FS $0}1' file

你可以摆脱引号以及 gsub /,,$ 2),但不确定删除它们,因为其余字段都被引用。

you can get rid of the quotes as well with gsub(/"/,"",$2) when assigning sn and loc, but not sure to remove them since the rest of the fields are quoted.

这篇关于将多个标题信息字段附加到文件,直到找到下一个标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆