追加多个标题信息字段到文件,直到find下一个标题

我正在尝试使用bashparsing许多日志文件。 日志文件看起来像这样:

"",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 

我想追加到每一行的序列号和位置(也许还有其他人),直到到达下一个头,并将其输出到master.csv文件。 该文件应该看起来像这样最终:

 "",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处理类似的文件:

Bash追加标题信息到文件的每一行,直到find下一个标题

这个oneliner非常适合查找标题,将其存储在持有空间中,并将其添加到每行的前面

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

这种方法没有用于将多个string附加到前面,因为我不确定如何在sed中使用多个保留空间。 另外,我不确定sed是否是最好的方法。 我不是很熟悉sed所以任何指针将不胜感激。

awk来拯救!

假设你的数据是一致的

 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 

当分配sn和loc时,你可以用gsub(/"/,"",$2)来除掉引号,但是由于其余字段被引用,所以不能确定删除它们。

sed中只有一个存储空间,但在这种情况下不需要多个存储空间:

 /^"Serial No."/ { # If we are on the "Serial No." line... N # Append next line to pattern space h # Copy pattern space to hold space # Remove everything but location and serial number from pattern space s/"[^"]*","([^"]*)"\n"[^"]*","([^"]*)"/\1,\2,/ x # Swap pattern space and hold space } /^"[[:digit:]]/ { # We are on a line where we want to prepend our data G # Append hold space to pattern space s/(.*)\n(.*)/\2\1/ # Move hold space content to front of pattern space } 

如果这是存储在一个文件sedscr.sed ,它可以被称为像

 sed -E -f sedscr.sed infile 

不会像示例输入/输出中显示的那样删除双引号; 它也假设数据应该被预先加入的行是带有日期的行,即以双引号和数字开始。