【发布时间】:2021-03-12 05:43:54
【问题描述】:
我在本地有配置文件,我从不同的远程机器上附加了一些变量。文件内容为:
#!/bin/bash
name=bob
department=(Production)
name=alice
department=(R&D)
name=maggie
department=(Production R&D)
文件中更新的最新值是最后一个。所以配置文件中的预期输出应该是:
#!/bin/bash
name=maggie
department=(Production R&D)
我想删除姓名和地址的前两个数据,除了最后一个。但是只有当有多个相同的变量时才会发生这种情况。
我参考并尝试了这个解决方案,但没有得到预期的输出: https://backreference.org/2011/11/17/remove-duplicates-but-keeping-only-the-last-occurrence/
【问题讨论】:
-
所以不是追加,而是覆盖文件。
-
不清楚您在寻找什么。您能否提供一个示例,说明您在更新前看到的内容以及更新后的预期内容?
-
department=(Production R&D)是语法错误。你需要一些引号。 -
is getting appended from the remote hosts where I cannot access the file to overwrite如果你可以追加(你对文件有写权限),你可以覆盖(在 99.99% 的情况下)。Appending is done by redirecting the sshpass output the file太好了!所以不要使用>>,而是使用>。
标签: bash shell duplicates