简体   繁体   English

Unix:使用sed替换多行字符串

[英]Unix: using sed to replace a multi-line string

I have a text file like this: 我有一个这样的文本文件:

**I**(0,80041,11.03.2014-15:05:05:702+3600,32.IROUTED,317435,iptdm.dtag,0,153452069,241769,80041,1,#00000000000000000000000000000000){
A(**11.03.2014-15:04:59:492+3600**){a=**S9**,0102,,,,,,,,,,,{0,,;},,,,**1.2.3.4:5060**,,,,,,,,,;c=0,17;n=S9;p=255;s=sip;b=;e=0;x=,+498003301000,5.6.7.8,5060,,,,08003301000,sip.test.de,,,tag\=00E0F510024703F9633D883C7650,,+492214711,sip.test.de,,,tag\=as0cd2768d,,+492214711,9.8.7.6,5060,,,,+492214711,sip.test.de,,,,,,,,,,,,,,,,,,,,,,,,1.2.3.4,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,;mlpp=;o=,,,0,15,0;}
C(**11.03.2014-15:05:02:237+3600**){a=S9,0102,,,,,,,D001,,,,{0,,;},,,,,,LNP_FILLED,RAD_OK,iiap492214711,,**iiai492214711**,,**ii498003301000**,;c=0,17;d=3465;n=S9;p=255;s=sip;x=,+498003301000,5.6.7.8,5060,,,,08003301000,sip.test.de,,,tag\=00E0F510024703F9633D883C7650,,+492214711,sip.test.de,,,tag\=as0cd2768d,,+492214711,9.8.7.6,5060,,,,+492214711,sip.test.de,,,,,,,,,,,,,,,,,,,,,,,,1.2.3.4,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,;mlpp=;o=11,D001,,2,15,0;}
V(11.03.2014-15:05:05:702+3600){i=498003301000;k=teles;l=21f6f14f-a926-11e3-ad91-00e0f5100247,21f6f14f-a926-11e3-ad91-00e0f5100247,;m=toner,voip;q=9.8.7.6,g711alaw;qc=1,,0,g711alaw:g711ulaw:g723r63:g723ar63:g729r8:g729ar8:g729br8:g729abr8:clearmode,echo,auto,30,7,32,27,32,0,0,0,20,4294967295,rfc2833,96,red,force,both,,,,;qi=3,1.2.3.4,5060,9.8.7.6,5060,g711alaw;qs=2,132,172,21120,27520,4294967295,0,4294967295,0,0;tud=,D001,,,,;ivr=,;h323=,,,,0:0:0;sip=4b211f615c1edd2e19ee10296df8a177@sip.test.de;}
D(**11.03.2014-15:05:05:702+3600**){f=A,CAU_NCC,LOC_PUBL_NTWK_SERV_REM_USER,CAU_NCC,LOC_PUBL_NTWK_SERV_REM_USER;t=S,CAU_NCC,LOC_PUBL_NTWK_SERV_REM_USER,CAU_NCC,LOC_PUBL_NTWK_SERV_REM_USER;a=S9,0102,,,,,,,D001,,,,{0,,;},,,,,,LNP_FILLED,RAD_OK,iiap492214711,,iiai492214711,,ii498003301000,;x=,+498003301000,5.6.7.8,5060,,,,08003301000,sip.test.de,,,tag\=00E0F510024703F9633D883C7650,,+492214711,sip.test.de,,,tag\=as0cd2768d,,+492214711,9.8.7.6,5060,,,,+492214711,sip.test.de,,,,,,,,,,,,,,,,,,,,,,,,1.2.3.4,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,;o=11,D001,,2,15,0;}
E(11.03.2014-15:05:05:702+3600){g=;}
}

I want to grep some fields of every leg and put them into a csv file with sed or maybe awk: 我想grep每条腿的某些字段,然后将它们放入sed或awk的csv文件中:

I,11.03.2014-15:04:59:492+3600,S9,1.2.3.4:5060,11.03.2014-15:04:59:492+3600,iiai492214711,ii498003301000,11.03.2014-15:05:05:702+3600

My attempt: 我的尝试:

sed 's/\([IO]\)(.*){
A(\(.*\)){a=\(.*\),.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,{.*,.*,.*;},.*,.*,.*,\(.*\),.*}
.*
C(\(.*\)){a=.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,{.*,.*,.*;},.*,.*,.*,.*,.*,.*,.*,.*,.*,\(.*\),.*,\(.*\),.*}
D(\(.*\)){.*}
.*
}/\1,\2,\3,\4,\5,\6,\7,\8' file

How can I use sed to use this regex string about more than one line? 如何使用sed将此正则表达式字符串使用多于一行?

Try something like this 试试这个

sed -n '/[*][*]I.*{/,/^}/ {
   /^[*][*]I/ {s/^[^,]*,\([^,]*\),.*/I,\1/;h;}
   /^A/   {s/^[^=]=[*][*]\([^*]*\)[*][*]\.*[*][*]\([^*]*\)[*][*].*/\1,\2/;H;}
   /^B/   {#extract what you want on this line }
   ...
   /^}/ {g;s/\n/,/p;}
   }' YourFile

You extract on each portion of line (of the I group) the wanted info, separate with , , add it to the hold buffer and at the end, recall the buffer (of each line) and replace the new line with a , then prnt the result 您提取的有用信息(在I组)行的每一个部分,有独立的,其添加到保持缓冲区和结束,召回(各行)的缓冲区,并更换了新的生产线,然后PRNT结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM