Bash
sed 與 awk:逐行讀取文件並附加到另一個文件中的特定行
考慮以下兩個文件
users.txt
:Arnold.Bosch David.Ledru Mehri.Sunny
和
docker-compose.txt
:version: '3.9' services: strongswan-server: image: vpn-strongswan container_name: strongswan-server networks: strongswan-server: environment: - STRONGSWAN_CLIENTS= sysctls: volumes: ports: - 500:500/udp - 4500:4500/udp volumes: strongswan-server-conf: networks: strongswan-server:
以下腳本使用
sed
過濾器將新使用者添加到包含STRONGSWAN_CLIENTS
.#!/bin/bash while IFS= read -r line; do echo "Text read from file: $line" sed -i "10s/$/ $line/" $2 done < "$1"
environment: - STRONGSWAN_CLIENTS=Arnold.Bosch David.Ledru Mehri.Sunny
我想知道是否有另一種方法可以基於
awk
?
這就是你說你想做的事情和你的程式碼做的事情,即附加到 STRONGSWAN_CLIENTS 行的末尾:
$ awk 'NR==FNR{users=users OFS $0; next} /STRONGSWAN_CLIENTS=/{$0=$0 users} 1' users.txt docker-compose.txt version: '3.9' services: strongswan-server: image: vpn-strongswan container_name: strongswan-server networks: strongswan-server: environment: - STRONGSWAN_CLIENTS=users Arnold.Bosch David.Ledru Mehri.Sunny sysctls: volumes: ports: - 500:500/udp - 4500:4500/udp volumes: strongswan-server-conf: networks: strongswan-server: