AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 694674
Accepted
Sergio Jimenez
Sergio Jimenez
Asked: 2022-03-17 10:55:33 +0800 CST2022-03-17 10:55:33 +0800 CST 2022-03-17 10:55:33 +0800 CST

在linux中的文件中使一个重复的字符串唯一

  • 772

我有一个这样的文件prueba.ldif:

dn: EpsStaInfId=EpsStaInf,serv=EPS,mscId=0015f5e3d05d4d52b0cb85db69474db3,ou=multiSCs,dc=three
structuralObjectClass: EpsStaticInf
objectClass: EpsStaticInf
entryDS: 1
nodeId: 21
createTimestamp: 20220303153032Z
modifyTimestamp: 20220303153032Z
EpsStaInfId: EpsStaInf
EpsProfileId: 10
EpsOdb: 0
EpsRoamAllow: TRUE
CDC: 1
EpsIndDefContextId: 1
EpsIndAmbrMaxUl: 320000000
EpsIndAmbrMaxDl: 1024000000
EpsRoamRestrict: TRUE
EpsTenantId: 1
EpsIndContextId: 1
EpsIndContextId: 2

dn: EpsStaInfId=EpsStaInf,serv=EPS,mscId=0040fb1140104f9fbc4be38be3db5965,ou=multiSCs,dc=three
structuralObjectClass: EpsStaticInf
objectClass: EpsStaticInf
entryDS: 1
nodeId: 21
createTimestamp: 20220301120221Z
modifyTimestamp: 20220301120221Z
EpsStaInfId: EpsStaInf
EpsProfileId: 10
EpsOdb: 0
EpsRoamAllow: TRUE
CDC: 1
EpsIndDefContextId: 1
EpsIndAmbrMaxUl: 320000000
EpsIndAmbrMaxDl: 1024000000
EpsRoamRestrict: TRUE
EpsTenantId: 1
EpsIndContextId: 1
EpsIndContextId: 5
EpsIndContextId: 15

我想为每个dn设置唯一的EpsIndContextId,在末尾添加一个数字,结果得到一个像这样的文件:

dn: EpsStaInfId=EpsStaInf,serv=EPS,mscId=0015f5e3d05d4d52b0cb85db69474db3,ou=multiSCs,dc=three
structuralObjectClass: EpsStaticInf
objectClass: EpsStaticInf
entryDS: 1
nodeId: 21
createTimestamp: 20220303153032Z
modifyTimestamp: 20220303153032Z
EpsStaInfId: EpsStaInf
EpsProfileId: 10
EpsOdb: 0
EpsRoamAllow: TRUE
CDC: 1
EpsIndDefContextId: 1
EpsIndAmbrMaxUl: 320000000
EpsIndAmbrMaxDl: 1024000000
EpsRoamRestrict: TRUE
EpsTenantId: 1
EpsIndContextId1: 1
EpsIndContextId2: 2

dn: EpsStaInfId=EpsStaInf,serv=EPS,mscId=0040fb1140104f9fbc4be38be3db5965,ou=multiSCs,dc=three
structuralObjectClass: EpsStaticInf
objectClass: EpsStaticInf
entryDS: 1
nodeId: 21
createTimestamp: 20220301120221Z
modifyTimestamp: 20220301120221Z
EpsStaInfId: EpsStaInf
EpsProfileId: 10
EpsOdb: 0
EpsRoamAllow: TRUE
CDC: 1
EpsIndDefContextId: 1
EpsIndAmbrMaxUl: 320000000
EpsIndAmbrMaxDl: 1024000000
EpsRoamRestrict: TRUE
EpsTenantId: 1
EpsIndContextId1: 1
EpsIndContextId2: 5
EpsIndContextId3: 15

我怎样才能做到这一点?

sed
  • 3 3 个回答
  • 50 Views

3 个回答

  • Voted
  1. Stéphane Chazelas
    2022-03-17T11:01:30+08:002022-03-17T11:01:30+08:00

    与perl:

    perl -pe '$i = 0 if /^dn:/; s/^EpsIndContextId\K/++$i/e' < prueba.ldif
    

    或编辑文件in-place:

    perl -i -pe '$i = 0 if /^dn:/; s/^EpsIndContextId\K/++$i/e' prueba.ldif
    

    上面,每当遇到以 开头的行时,我们都会重置计数器dn:。您可以if /^dn:/改为if /^$/搜索空行或unless /\S/搜索空行(仅由空白字符组成的行),或者正如@glennjackman 建议的那样,使用段落模式,-00其中记录而不是行,由序列分隔一个或多个空行(2 个或多个换行符),并使用m替换中的标志^来匹配主题(段落)中每一行的开头,而不是仅在主题的开头和g标志替换每个记录中出现:

    perl -00 -pe '$i = 0; s/^EpsIndContextId\K/++$i/emg' < prueba.ldif
    
    • 2
  2. DanieleGrassini
    2022-03-17T13:13:57+08:002022-03-17T13:13:57+08:00

    POSIX awk:

    awk '/^dn: /{c=0} /^EpsIndContextId/{sub(/^EpsIndContextId/, "&"++c)} 1' test
    

    重置计数器 ( c) 和每个部分 ( ) 的开头dn,因此每次找到一个EpsIndContextId它都会增加计数器并将其添加到此。

    或者,@Ed Morton建议:

    awk '/^dn: /{c=0} sub(/^EpsIndContextId/, "&"c+1){c++} 1' test
    

    避免使用相同的正则表达式两次。

    • 0
  3. Best Answer
    fuzzydrawrings
    2022-03-17T13:28:53+08:002022-03-17T13:28:53+08:00

    这是一个sed解决方案:

    sed -E 's/(^EpsIndContextId)(:) (.*$)/\1\3\2 \3/' prueba.ldif

    • 0

相关问题

  • Linux grep文件1中的内容在文件2中[重复]

  • 如何在第三个逗号后用条件grep行

  • 根据第一个逗号之前的匹配删除重复行数

  • 如何改进这个字符转换脚本?

  • 如何删除两行之间的单行

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve