交换文件中的行

交换文件中的行

我有一个这样的文件:

DelegatedAdmin: cn=fb6190f7-4a33-4502-a2d5-dce8dc434570,ou=Named,ou=Identities,ou=Active,ou=Vault<br />
changetype: modify<br />
delete: DelegatedAdmin<br />
dn: cn=311999,ou=Entities,ou=Active,ou=Vault<br />

DelegatedAdmin: cn=fb6190f7-4a33-4502-a2d5-dce8dc434570,ou=Named,ou=Identities,ou=Active,ou=Vault<br />
changetype: modify<br />
delete: DelegatedAdmin<br />
dn: cn=948089,ou=Entities,ou=Active,ou=Vault<br />

DelegatedAdmin: cn=fb6190f7-4a33-4502-a2d5-dce8dc434570,ou=Named,ou=Identities,ou=Active,ou=Vault<br />
changetype: modify<br />
delete: DelegatedAdmin<br />
dn: cn=947458,ou=Entities,ou=Active,ou=Vault<br />

DelegatedAdmin: cn=9776c3f8-f2ee-4b6c-a9a9-32d87b45f7c5,ou=Named,ou=Identities,ou=Active,ou=Vault<br />
changetype: modify<br />
delete: DelegatedAdmin<br />
dn: cn=10717,ou=Entities,ou=Active,ou=Vault<br />

DelegatedAdmin: cn=e0a838f0-3d53-42e8-8dad-bd1d774381de,ou=Named,ou=Identities,ou=Active,ou=Vault<br />
changetype: modify<br />
delete: DelegatedAdmin<br />
dn: cn=1500,ou=Entities,ou=Active,ou=Vault<br />

并且应该改为这样(交换第1行和第4行)

dn: cn=311999,ou=Entities,ou=Active,ou=Vault<br />
changetype: modify<br />
delete: DelegatedAdmin<br />
DelegatedAdmin: cn=fb6190f7-4a33-4502-a2d5-dce8dc434570,ou=Named,ou=Identities,ou=Active,ou=Vault<br />

dn: cn=948089,ou=Entities,ou=Active,ou=Vault<br />
changetype: modify<br />
delete: DelegatedAdmin<br />
DelegatedAdmin: cn=fb6190f7-4a33-4502-a2d5-dce8dc434570,ou=Named,ou=Identities,ou=Active,ou=Vault<br />

dn: cn=947458,ou=Entities,ou=Active,ou=Vault<br />
changetype: modify<br />
delete: DelegatedAdmin<br />
DelegatedAdmin: cn=fb6190f7-4a33-4502-a2d5-dce8dc434570,ou=Named,ou=Identities,ou=Active,ou=Vault<br />

dn: cn=10717,ou=Entities,ou=Active,ou=Vault<br />
changetype: modify<br />
delete: DelegatedAdmin<br />
DelegatedAdmin: cn=9776c3f8-f2ee-4b6c-a9a9-32d87b45f7c5,ou=Named,ou=Identities,ou=Active,ou=Vault<br />

dn: cn=1500,ou=Entities,ou=Active,ou=Vault<br />
changetype: modify<br />
delete: DelegatedAdmin<br />
DelegatedAdmin: cn=e0a838f0-3d53-42e8-8dad-bd1d774381de,ou=Named,ou=Identities,ou=Active,ou=Vault<br />

这个怎么做?

答案1

也试试

awk '{print $4, $2, $3, $1}' RS= FS="\n" OFS="\n" ORS="\n\n"  file
dn: cn=311999,ou=Entities,ou=Active,ou=Vault<br />
changetype: modify<br />
delete: DelegatedAdmin<br />
DelegatedAdmin: cn=fb6190f7-4a33-4502-a2d5-dce8dc434570,ou=Named,ou=Identities,ou=Active,ou=Vault<br />

使用RS=<empty>由空行分隔的多行记录,字段分隔<NL>符使行 = 字段,然后按所需的顺序打印它们。

答案2

采取不同的策略,你可以ed用来越过DelegatedAdmin:向下 3 行和dn:向上 3 行:

printf '%s\n' 'g/^DelegatedAdmin:/m+3' 'g/^dn:/m-3' 'w newfile' 'q' | ed -s file

更改'w newfile' 'q'为纯文本以就地'wq'编辑。file测试标准输出:

$ printf '%s\n' 'g/^DelegatedAdmin:/m+3' 'g/^dn:/m-3' ',p' 'Q' | ed -s file
dn: cn=311999,ou=Entities,ou=Active,ou=Vault<br />
changetype: modify<br />
delete: DelegatedAdmin<br />
DelegatedAdmin: cn=fb6190f7-4a33-4502-a2d5-dce8dc434570,ou=Named,ou=Identities,ou=Active,ou=Vault<br />

dn: cn=948089,ou=Entities,ou=Active,ou=Vault<br />
changetype: modify<br />
delete: DelegatedAdmin<br />
DelegatedAdmin: cn=fb6190f7-4a33-4502-a2d5-dce8dc434570,ou=Named,ou=Identities,ou=Active,ou=Vault<br />

dn: cn=947458,ou=Entities,ou=Active,ou=Vault<br />
changetype: modify<br />
delete: DelegatedAdmin<br />
DelegatedAdmin: cn=fb6190f7-4a33-4502-a2d5-dce8dc434570,ou=Named,ou=Identities,ou=Active,ou=Vault<br />

dn: cn=10717,ou=Entities,ou=Active,ou=Vault<br />
changetype: modify<br />
delete: DelegatedAdmin<br />
DelegatedAdmin: cn=9776c3f8-f2ee-4b6c-a9a9-32d87b45f7c5,ou=Named,ou=Identities,ou=Active,ou=Vault<br />

dn: cn=1500,ou=Entities,ou=Active,ou=Vault<br />
changetype: modify<br />
delete: DelegatedAdmin<br />
DelegatedAdmin: cn=e0a838f0-3d53-42e8-8dad-bd1d774381de,ou=Named,ou=Identities,ou=Active,ou=Vault<br />

答案3

您可能会在现有线程中找到更好的答案:
更改文件中的行顺序

看来那里有一些非常好的答案。如果您需要“就地”完成此操作,这可能没有帮助;但我使用以下方法从那里推断出以下解决方案Perl

https://unix.stackexchange.com/a/368429/344329

该脚本处理整个文件,对于每一行(存储在 $_ 中),它将获取接下来的两行($l2 和 $l3)并按请求的顺序打印它们:line1、line3、line2。

那里的解决办法是:

perl -ne '$l2=<>; $l3=<>; print $_,$l3,$l2;' input.txt

对于您的用例,您可以尝试这样的事情:

perl -ne '$l2=<>; $l3=<>; $l4=<>; $l5=<>; print $l4,$l2,$l3,$_,$l5; if(eof) { exit; } ' input.txt >> output.txt

坦白说:我不是专家,所以我不确定这对您来说是最好的方法。

答案4

sed

sed '/^Delegate/{N;N;N;s/\(.*\)\(\n.*\n.*\n\)\(.*\)/\3\2\1/;}'

或者,利用保留空间:

sed -ne '/^Delegate/{h;n;N;G;x;n;G;}' -e p

相关内容