使用sed或awk提取2个模式之间的线条

时间:2015-05-08 09:44:41

标签: bash awk sed

我是shell脚本的新手,并尝试使用旧线程从日志文件中检索消息,但未能获得所需的输出。

下面是示例消息的样子

00:31:54.184 MNK  I 4155809232 (monklog:391): The result of the mapping is : S|aaaaa|bbbbb|32|D|M|28/04/2015|ccc|33208369
00:31:54.184 MNK  I 4155809232 (monklog:391): .05|28/04/2015|0428|C|105840.|dddd|fffff|9511705558|/CTC/097/eeeeee eee|/PT
00:31:54.184 MNK  I 4155809232 (monklog:391): /SC/TT/12/SN/eee eeeeeee/CeeY/ee -eee aa aaaa S.A.B. DE C.V./DC/aaaaa
00:31:54.184 MNK  I 4155809232 (monklog:391):  AND aaaaa aaaa/NA/aaaaa,/SK/aaaaa|D|M|28/04/2015|MXN|11111.17||||
00:31:54.184 MNK  I 4155809232 (monklog:391): ||||ssssss|ssssss|qwerrt-aaaaaa|ggggggg||||||||||||||||
00:31:54.184 MNK  I 4155809232 (monklog:391): S|aaaaa|bbbbb|32|D|M|28/04/2015|ccc|33208369
00:31:54.184 MNK  I 4155809232 (monklog:391): .05|28/04/2015|0428|C|105840.|dddd|fffff|9511705558|/CTC/097/eeeeee eee|/PT
00:31:54.184 MNK  I 4155809232 (monklog:391): /SC/TT/12/SN/eee eeeeeee/CeeY/ee -eee aa aaaa S.A.B. DE C.V./DC/aaaaa
00:31:54.184 MNK  I 4155809232 (monklog:391):  AND aaaaa aaaa/NA/aaaaa,/SK/aaaaa|D|M|28/04/2015|MXN|11111.17||||
00:31:54.184 MNK  I 4155809232 (monklog:391): ||||ssssss|ssssss|qwerrt-aaaaaa|ggggggg||||||||||||||||
00:31:54.184 MNK  I 4155809232 (monklog:406): ||29/04/2015 01:31:00|||||||||^M

我需要从 S | ^ M 之前收到消息。

我试过这些代码。

awk '/S|/{flag=1}/|^M/{flag=0}flag' $Log  > output2.txt
sed -n '/: S|/,/|^M/p' $Log > output.txt

两者都给我与输出相同的输入。请帮忙。感谢。

预期输出

S|aaaaa|bbbbb|32|D|M|28/04/2015|ccc|33208369.05|28/04/2015|0428|C|105840.|dddd|fffff|9511705558|/CTC/097/eeeeee eee|/PT/SC/TT/12/SN/eee eeeeeee/CeeY/ee -eee aa aaaa S.A.B. DE C.V./DC/aaaaa AND aaaaa aaaa/NA/aaaaa,/SK/aaaaa|D|M|28/04/2015|MXN|11111.17||||||||ssssss|ssssss|qwerrt-aaaaaa|ggggggg||||||||||||||||
S|aaaaa|bbbbb|32|D|M|28/04/2015|ccc|33208369.05|28/04/2015|0428|C|105840.|dddd|fffff|9511705558|/CTC/097/eeeeee eee|/PT/SC/TT/12/SN/eee eeeeeee/CeeY/ee -eee aa aaaa S.A.B. DE C.V./DC/aaaaa AND aaaaa aaaa/NA/aaaaa,/SK/aaaaa|D|M|28/04/2015|MXN|11111.17||||||||ssssss|ssssss|qwerrt-aaaaaa|ggggggg||||||||||||||||

每一组都应该单行。

2 个答案:

答案 0 :(得分:0)

This works for your exact specifications of input and output

Dim sShape As Shape
Set sShape = ActiveSheet.Shapes(Application.Caller)

With sShape.Fill
    If .Visible = True Then
        .Visible = False
        With ActiveSheet.Shapes.Range(Array("Rectangle 109")).Fill
        .Visible = True
        .ForeColor.RGB = RGB(255, 0, 0)
        End With
    Else
        .Visible = True
        .ForeColor.RGB = RGB(255, 0, 0)
        With ActiveSheet.Shapes.Range(Array("Rectangle 109")).Fill
        .Visible = False
        End With
    End If
End With

答案 1 :(得分:0)

基于{p> sed的方法:

$ sed -n '/S\|/,/\^M/{

           /S\|/  {s/.*S|/S|/};
                  {s/.*[0-9]\+): //;H}
           /\^M/  {g;s/\n//g;s/\^M.*//p;};

       }' file.log
S|aaaaa|bbbbb|32|D|M|28/04/2015|ccc|33208369.05|28/04/2015|0428|C|105840.|dddd|fffff|9511705558|/CTC/097/eeeeee eee|/PT/SC/TT/12/SN/eee eeeeeee/CeeY/ee -eee aa aaaa S.A.B. DE C.V./DC/aaaaa AND aaaaa aaaa/NA/aaaaa,/SK/aaaaa|D|M|28/04/2015|MXN|11111.17||||||||ssssss|ssssss|qwerrt-aaaaaa|ggggggg||||||||||||||||S|aaaaa|bbbbb|32|D|M|28/04/2015|ccc|33208369.05|28/04/2015|0428|C|105840.|dddd|fffff|9511705558|/CTC/097/eeeeee eee|/PT/SC/TT/12/SN/eee eeeeeee/CeeY/ee -eee aa aaaa S.A.B. DE C.V./DC/aaaaa AND aaaaa aaaa/NA/aaaaa,/SK/aaaaa|D|M|28/04/2015|MXN|11111.17||||||||ssssss|ssssss|qwerrt-aaaaaa|ggggggg||||||||||||||||00|||||||||

说明:

  1. 适用于S|& ^M - '/S\|/,/\^M/{
  2. 如果该行包含S|,则删除所有内容,直至S| - /S\|/{s/.*S|/S|/};
  3. 删除所有内容,直到<digits>):&amp;将剩余的字符串附加到空格 - {s/.*[0-9]\+): //;H}。这将删除前缀文本,如00:31:54.184 MNK I 4155809232 (monklog:391):
  4. 对于匹配^M的行,将整个保留空间复制到图案空间。删除换行符(由于H命令而添加了换行符。删除^M&amp; print后的所有内容。 - /\^M/ {g;s/\n//g;s/\^M.*//p;};
  5. 使用awk

    的类似逻辑
    $ awk -v FS="[0-9]+): " '
         /S\|/ && (!a){  a = a gensub(/.*S\|/,"S|","",$2); next;}
         /\^M/ && a { print a gensub(/\^M.*/,"","",$2); a=0;}
         a{a=a $2};
         ' file.log