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



我是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||||||||||||||||

每组应为单行。

这是你喜欢的吗?

awk -F"): " '{$0=$NF} /^S|/ {f=1} /^M/ {f=0} f' file
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|开始。您有一个行1和一个行6(之后都有相同的数据)

这适用于输入和输出的确切规格

awk '{$0=substr($0,47)}/^S|/{x=1}/^M$/{x=0}x' file
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||||||||||||||||

基于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>):并附加剩余的字符串以容纳空间 -{s/.*[0-9]+): //;H}.这将删除前缀文本,例如00:31:54.184 MNK I 4155809232 (monklog:391):
  4. 对于与^M匹配的行,将整个保留空间复制到图案空间。删除换行符(由于H命令而添加的换行符。^M和打印后删除所有内容。-/^M/ {g;s/n//g;s/^M.*//p;};

使用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 

相关内容

  • 没有找到相关文章

最新更新