【问题标题】:find the second word in first line which starts with specific word and replace the next line first word in linux在第一行中找到以特定单词开头的第二个单词并替换下一行在linux中的第一个单词
【发布时间】:2014-10-10 10:01:09
【问题描述】:

我需要将以下文件更改为新文件...

我随身携带的文件....

uid=GESCHELP1, ou=Special Users, dc=domain,dc=com
different: c
        1: IND
different: sn
        1: REC

uid=GESCHELP2, ou=Special Users, dc=domain,dc=com
different: country
        1: USA
different: givenname
        1: Raj

输出文件应该如下......

dn: uid=GESCHELP1, ou=Special Users, dc=domain,dc=com
changetype: modify
replace: c
c: IND
-
replace: sn
sn: REC

uid=GESCHELP2, ou=Special Users, dc=domain,dc=com
changetype: modify
replace: country
country: USA
-
replace: givenname
givenname: Aus

请帮我解决。

【问题讨论】:

  • StackOverflow 不是代码编写服务 - 请先提供您尝试过的任何解决方案。
  • 让我“帮助 [you] 获得解决方案。”:您可能需要查看 sedawkbash-builtins 的手册页
  • 我是脚本初学者...到目前为止,我已经使用以下命令来获取以下文件...
  • sed -n -e '/2/!p' -e '/uid/a changetype: modify' -e '/1/a -' user.ldif sed -i 's/different /replace/g' user.ldif 我得到了以下输出...从这里我无法替换字符串.. uid=GESCHELP1, ou=Special Users, dc=domain,dc=com changetype: modify - replace: c 1:IND - 替换:sn 1:REC - 更改类型:修改替换:国家 1:美国 - 替换:给定名称 1:Raj -
  • @EugenRieck ....我的任何更新..@Rakesh

标签: linux shell


【解决方案1】:

此命令将给出所需的结果

awk -F': *' -v OFS=": " '$1=="replace"{r=$2;print;next}r{$1=r;r=""}7' 文件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-15
    • 2022-11-03
    • 1970-01-01
    相关资源
    最近更新 更多