如何搜索模式文本并在一行中转置,直到下次找到相同的文本?

时间:2017-04-07 13:03:41

标签: awk

我正在尝试使用方括号中的时间戳标头解析日志文件。如果找到方括号,那么它下面的所有线都将被转置,直到下次找到相同的方括号? 我有这种格式的数据:

[2017-03-31 01:15:08.000] LTEEvent
Event {
recordLength 4,
recordType 0,
FileHeader {
file-format-version T,
pm-recording-version C,
pm-recording-revision R,
year 2,
month 3,
day 3,
hour 1,
minute 1,
second 8,
ne-user-label LD,
ne-logical-name LD
}
} 
[2017-03-31 01:15:08.000] LTEEvent
Event {
recordLength 1,
recordType 3,
ScannerConnection {
hour 1,
minute 1,
second 8,
millisecond 0,
scanner-id '0'H,
status 2,
padding '0'H
}
}
and so on......The number of lines after[Date Time] LTE Event is not fixed and keeps on changing

我正在寻找excel中的输出:

[2017-03-31 01:15:08.000] LTEEvent  LTEEvent {  recordLength 417    
[2017-03-31 01:15:08.000] LTEEvent  LTEEvent {      

3 个答案:

答案 0 :(得分:1)

更多

<强>输入

$ cat file
[2017-03-31 01:15:08.000] LTEEvent
LTEEvent {
recordLength 417,
[2017-03-31 01:15:08.000] LTEEvent
LTEEvent {

<强>输出

$ awk -v RS=, '{ $1=$1 }1' file
[2017-03-31 01:15:08.000] LTEEvent LTEEvent { recordLength 417
[2017-03-31 01:15:08.000] LTEEvent LTEEvent {

<强>解释

  • -v RS=,记录分隔符设置为逗号

  • $1=$1 当您执行$1=$1(或对字段的任何其他分配)时,它会导致重建$0的记录重新编译,每FS替换为OFS,因此它将设置输出字段分隔符(OFS)为默认值(即单个空格),因此删除了换行符。

  • 1执行默认操作{ print $0 }

$ awk '/^\[/{if(s)print s;s=""}{sub(/,$/,"");s =(s ? s OFS:"") $0}END{if(s)print s}' file
[2017-03-31 01:15:08.000] LTEEvent LTEEvent { recordLength 417
[2017-03-31 01:15:08.000] LTEEvent LTEEvent {

<强>解释

awk '/^\[/{                               # search for line starts with [
         if(s)print s;s=""                # if variable s has something print and reset
     }
     {
       sub(/,$/,"");                      # remove comma before the line terminator from record
       s =(s ? s OFS:"") $0               # concatenate variable s with current record
     }
  END{                                    # end block
       if(s)print s                       # if s has something print 
     }' file

答案 1 :(得分:0)

尝试此操作(如果您的文件没有.*

$ echo $(cat f)|sed 's/\[/\n\[/g'

[2017-03-31 01:15:08.000] LTEEvent LTEEvent { recordLength 417,
[2017-03-31 01:15:08.000] LTEEvent LTEEvent {

编辑1:使用新输入。

awk '{printf "%s ", $0} END {printf "\n"}' f | sed 's/\[201/\n\[201/g' |nl

     1  [2017-03-31 01:15:08.000] LTEEvent Event { recordLength 4, recordType 0, FileHeader { file-format-version T, pm-recording-version C, pm-recording-revision R, year 2, month 3, day 3, hour 1, minute 1, second 8, ne-user-label LD, ne-logical-name LD } }  
     2  [2017-03-31 01:15:08.000] LTEEvent Event { recordLength 1, recordType 3, ScannerConnection { hour 1, minute 1, second 8, millisecond 0, scanner-id '0'H, status 2, padding '0'H } } and so on......The number of lines after[Date Time] LTE Event is not fixed and keeps on changing 

如果您获得预期的输出,请在命令末尾替换|nl

答案 2 :(得分:0)

在awk中被滥用:

array