首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用每个文件的L10字符7-11中的值替换多个文件中的模式

用每个文件的L10字符7-11中的值替换多个文件中的模式
EN

Stack Overflow用户
提问于 2015-09-22 12:13:52
回答 2查看 300关注 0票数 0

大家好,

我想知道您是否可以帮助我处理UNIX命令/命令集,这些命令修改当前目录中的所有".mos“文件(其中只包含文本行)如下:我想用.mos替换每个文件的"ExternalENodeBFunction=1”条目

ID是从每个文件的第10行字符7-11读取的.我知道我可以使用sed命令来用其他字符串替换模式,或者使用awk命令,但是我不知道/理解如何用特定的字符串位置替换模式。

非常感谢你的帮助,

卡特林

PS --我添加了一个带有输入和期望输出的图片。

输入:

代码语言:javascript
复制
#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-

输出:

代码语言:javascript
复制
        #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-
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-22 13:44:08

你想要的是这样的东西:

代码语言:javascript
复制
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

但是它显然没有经过测试,因为您没有提供任何我们可以进行测试的示例输入/输出。

票数 1
EN

Stack Overflow用户

发布于 2015-09-22 12:57:37

代码语言:javascript
复制
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/.....//
   ' YourFile
  • 使用这个sed查找(或循环中的grep )将为您提供所有要转换的文件。
  • 假设有第10行包含信息(这里的脚本不是安全的,但可能很容易)
  • 需要在内存中加载文件(两次),所以不是最好的大型文件(不知道.mos的平均文件大小,希望它不是1GB)
  • 假设文件没有由外部结束以更改(如果需要可以修改),如样例中所示
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32716577

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档