SED删除重复的文件部分

时间:2014-07-03 15:42:16

标签: regex unix sed

我有下一个配置文件:

[DEFAULT]
SenderCompID=BARXPB
ConnectionType=acceptor
SocketAcceptPort=4444
FileStorePath=store
FileLogPath=/apps/test
HttpAcceptPort=3333
TransportDataDictionary=../../share/quickfix/FIXT11.xml
AppDataDictionary.FIX.4.0=../../share/quickfix/FIX40.xml
AppDataDictionary.FIX.4.1=../../share/quickfix/FIX41.xml
AppDataDictionary.FIX.4.2=../../share/quickfix/FIX42.xml
AppDataDictionary.FIX.4.3=../../share/quickfix/FIX43.xml
AppDataDictionary.FIX.4.4=../../share/quickfix/FIX44.xml
AppDataDictionary.FIX.5.0=../../share/quickfix/FIX50.xml
AppDataDictionary.FIX.5.0SP1=../../share/quickfix/FIX50SP1.xml
AppDataDictionary.FIX.5.0SP2=../../share/quickfix/FIX50SP2.xml
StartTime=00:00:00
EndTime=23:59:59
StartDay=sun
EndDay=sat

[SESSION]
TargetCompID=TUDOR-TEST
BeginString=FIX.4.4
DataDictionary=../../share/quickfix/FIX44.xml

[SESSION]
TargetCompID=SECOR-TEST
BeginString=FIX.4.4
DataDictionary=../../share/quickfix/FIX44.xml

[SESSION]
TargetCompID=ORTUS-TEST
BeginString=FIX.4.4
DataDictionary=../../share/quickfix/FIX44.xml

[SESSION]
TargetCompID=CRABEL-TEST
BeginString=FIXT.1.1
DefaultApplVerID=FIX.5.0SP2

[SESSION]
TargetCompID=FXCM-TEST
BeginString=FIXT.1.1
DefaultApplVerID=FIX.5.0SP2

[SESSION]
TargetCompID=HIROSEIIJ-TEST
BeginString=FIX.4.4
DataDictionary=../../share/quickfix/FIX44.xml

[SESSION]
TargetCompID=KNIGHT-TEST
BeginString=FIXT.1.1
DefaultApplVerID=FIX.5.0SP2

我的任务是更改所有[SESSION]标签,例如只有一个[SESSION]和CRABEL-TEST记录,所以最终的配置文件应如下所示:

[DEFAULT]
SenderCompID=BARXPB
ConnectionType=acceptor
SocketAcceptPort=4444
FileStorePath=store
FileLogPath=/apps/test
HttpAcceptPort=3333
TransportDataDictionary=../../share/quickfix/FIXT11.xml
AppDataDictionary.FIX.4.0=../../share/quickfix/FIX40.xml
AppDataDictionary.FIX.4.1=../../share/quickfix/FIX41.xml
AppDataDictionary.FIX.4.2=../../share/quickfix/FIX42.xml
AppDataDictionary.FIX.4.3=../../share/quickfix/FIX43.xml
AppDataDictionary.FIX.4.4=../../share/quickfix/FIX44.xml
AppDataDictionary.FIX.5.0=../../share/quickfix/FIX50.xml
AppDataDictionary.FIX.5.0SP1=../../share/quickfix/FIX50SP1.xml
AppDataDictionary.FIX.5.0SP2=../../share/quickfix/FIX50SP2.xml
StartTime=00:00:00
EndTime=23:59:59
StartDay=sun
EndDay=sat

[SESSION]
TargetCompID=CRABEL-TEST
BeginString=FIXT.1.1
DefaultApplVerID=FIX.5.0SP2

是否可以通过SED制作?我用过这个表达式

sed '/SESSION//CRABEL-TEST/,/SESSION/s/,SESSION,/,CRABEL-TEST,/g'

但没有运气。

提前致谢。

1 个答案:

答案 0 :(得分:1)

尝试以下GNU awk命令,

$ awk -v RS="\n\n" '/^\[DEFAULT\]/{print} /^\[SESSION\]/&&/CRABEL-TEST/{print}' ORS="\n\n" file
[DEFAULT]
SenderCompID=BARXPB
ConnectionType=acceptor
SocketAcceptPort=4444
FileStorePath=store
FileLogPath=/apps/test
HttpAcceptPort=3333
TransportDataDictionary=../../share/quickfix/FIXT11.xml
AppDataDictionary.FIX.4.0=../../share/quickfix/FIX40.xml
AppDataDictionary.FIX.4.1=../../share/quickfix/FIX41.xml
AppDataDictionary.FIX.4.2=../../share/quickfix/FIX42.xml
AppDataDictionary.FIX.4.3=../../share/quickfix/FIX43.xml
AppDataDictionary.FIX.4.4=../../share/quickfix/FIX44.xml
AppDataDictionary.FIX.5.0=../../share/quickfix/FIX50.xml
AppDataDictionary.FIX.5.0SP1=../../share/quickfix/FIX50SP1.xml
AppDataDictionary.FIX.5.0SP2=../../share/quickfix/FIX50SP2.xml
StartTime=00:00:00
EndTime=23:59:59
StartDay=sun
EndDay=sat

[SESSION]
TargetCompID=CRABEL-TEST
BeginString=FIXT.1.1
DefaultApplVerID=FIX.5.0SP2