-2

我正在尝试使用 bash 脚本使用 File2.conf 中的文本更新 File1.conf。

~/.group/File1.conf

group b = bill john
group a = bill amy john

./File2.conf

group b = bill amy cara mike joe
group c = bill steven mike larry

导致 ~/.group/File1.conf 看起来像

group b = bill amy cara mike joe
group a = bill amy john
group c = bill steven mike larry

我一直在看,awksed似乎无法得到正确的输出。唯一的常数是group $ =和 间距。

4

2 回答 2

1

看看这个sort

$ sort -t'=' -uk1,1 file2 file1
group a = bill amy john
group b = bill amy cara mike joe
group c = bill steven mike larry
于 2018-08-29T18:51:04.977 回答
0

我会写这个awk:

awk -F= 'NR == FNR {seen[$1]; print; next} !($1 in seen)' File{2,1}.conf

要将输出保存回 File1.conf,或者

  1. 使用临时文件:

    t=$(mktemp)
    awk '...' File{2,1}.conf > "$t" && mv "$t" File1.conf
    
  2. 安装moreutils包并使用sponge

    awk '...' File{2,1}.conf | sponge File1.conf
    
于 2018-08-29T18:54:28.467 回答