大家好,
我想知道您是否可以帮助我处理UNIX命令/命令集,这些命令修改当前目录中的所有".mos“文件(其中只包含文本行)如下:我想用.mos替换每个文件的"ExternalENodeBFunction=1”条目
ID是从每个文件的第10行字符7-11读取的.我知道我可以使用sed命令来用其他字符串替换模式,或者使用awk命令,但是我不知道/理解如何用特定的字符串位置替换模式。
非常感谢你的帮助,
卡特林
PS --我添加了一个带有输入和期望输出的图片。

输入:
#DoNotEditThisLine: =====================INPUT FILE==================
gs+
if $moshell_version ~ ^([7-9]|10)
l echo "The moshell version is too old. 11.0a or higher is required for scripts containing the crn command."
return
fi
crn ENodeBFunction=1,EUtraNetwork=1,ExternalENodeBFunction=1
eNBId 42712
eNodeBPlmnId
masterEnbFunctionId
mfbiSupport
userLabel
end
crn ENodeBFunction=1,EUtraNetwork=1,ExternalENodeBFunction=1,ExternalEUtranCellFDD=xxxx
activePlmnList
activeServiceAreaId
csgPhysCellId
csgPhysCellIdRange
eutranFrequencyRef EUtraNetwork=1,EUtranFrequency=1325
isRemoveAllowed false
lbEUtranCellOffloadCapacity
localCellId
masterEUtranCellFDDId xxxx
pciConflict
pciConflictCell
pciDetectingCell
physicalLayerCellIdGroup
physicalLayerSubCellId
tac
userLabel xxxx
end
crn ENodeBFunction=1,EUtranCellFDD=cell0042753L3,EUtranFreqRelation=1325,EUtranCellRelation=22299-42712-1
cellIndividualOffsetEUtran
coverageIndicator
includeInSystemInformation
isHoAllowed
isRemoveAllowed
lbBnrAllowed
loadBalancing
neighborCellRef EUtraNetwork=1,ExternalENodeBFunction=1,ExternalEUtranCellFDD=xxxx
qOffsetCellEUtran
sCellCandidate
end
gs-输出:
#DoNotEditThisLine: ===============OUTPUT FILE==================
gs+
if $moshell_version ~ ^([7-9]|10)
l echo "The moshell version is too old. 11.0a or higher is required for scripts containing the crn command."
return
fi
crn ENodeBFunction=1,EUtraNetwork=1,ExternalENodeBFunction=42712
eNBId 42712
eNodeBPlmnId
masterEnbFunctionId
mfbiSupport
userLabel
end
crn ENodeBFunction=1,EUtraNetwork=1,ExternalENodeBFunction=42712,ExternalEUtranCellFDD=xxxx
activePlmnList
activeServiceAreaId
csgPhysCellId
csgPhysCellIdRange
eutranFrequencyRef EUtraNetwork=1,EUtranFrequency=1325
isRemoveAllowed false
lbEUtranCellOffloadCapacity
localCellId
masterEUtranCellFDDId xxxx
pciConflict
pciConflictCell
pciDetectingCell
physicalLayerCellIdGroup
physicalLayerSubCellId
tac
userLabel xxxx
end
crn ENodeBFunction=1,EUtranCellFDD=cell0042753L3,EUtranFreqRelation=1325,EUtranCellRelation=xxxxx
cellIndividualOffsetEUtran
coverageIndicator
includeInSystemInformation
isHoAllowed
isRemoveAllowed
lbBnrAllowed
loadBalancing
neighborCellRef EUtraNetwork=1,ExternalENodeBFunction=42712,ExternalEUtranCellFDD=xxxx
qOffsetCellEUtran
sCellCandidate
end
gs-发布于 2015-09-22 13:44:08
你想要的是这样的东西:
awk '
NR < 10 {
buf[NR] = $0
}
NR == 10 {
eNBId = $2
for (i=1; i<NR; i++) {
sub(/ExternalENodeBFunction=[0-9]+/,"ExternalENodeBFunction="eNBId,buf[i])
print buf[i]
}
print
}
NR > 10 {
sub(/ExternalENodeBFunction=[0-9]+/,"ExternalENodeBFunction="eNBId)
print
}
' file但是它显然没有经过测试,因为您没有提供任何我们可以进行测试的示例输入/输出。
发布于 2015-09-22 12:57:37
sed ': load
# load lines (1 by 1) until Not last one
$ !{N;b load
}
# keep a copy into holding buffer
h
# Extracting the number to substitute
s/^\([[:alnum:][:blank:][:punct:]]*\n\)\{9\}.\{6\}\(....\).*/\2/
# add all the line after this (1st line is the number)
G
: repl
# replace last occurence of External with the new reference
s/^\(....\)\(.*\)\(ExternalENodeBFunction=\)1\([^0-9]\)/\1\2\3\1\4/
# if replacement occur, (re)try another one
t repl
# cleanning by removing first number (4 digit + 1 new line)
s/.....//
' YourFilehttps://stackoverflow.com/questions/32716577
复制相似问题