我需要從一個檔案中讀取值并將該值存盤在另一個檔案中。所以這是檔案的內容
檔案1.txt
test1.example.com
test2.example.com
test3.example.com
檔案2.txt
### list of servers ##
[group]
server1 ansible_host=pub_ip1 ansible_user=eadmin
server2 ansible_host=pub_ip2 ansible_user=eadmin
server3 ansible_host=pub_ip3 ansible_user=eadmin
所以預期的輸出應該低于
檔案2.txt
### list of servers ##
[group]
test1.example.com ansible_host=pub_ip1 ansible_user=eadmin
test2.example.com ansible_host=pub_ip2 ansible_user=eadmin
test3.example.com ansible_host=pub_ip3 ansible_user=eadmin
file1 的第一行應替換 file2 中的 server1,依此類推。我對 bash 和 Linux 非常陌生。如果有人可以幫助我實作這一目標,那就太好了。
提前致謝
uj5u.com熱心網友回復:
這可能對您有用(GNU sed):
sed -e '/[group]/!b;:a;n;R file1' -e 'ba' file2 |
sed '1,/[group]/b;N;s/\n\S\ \s*/ /'
在第一次 sed 呼叫中將 file1 與 file2 交錯。
將結果通過管道傳遞給第二個 sed 呼叫,該呼叫將第一個欄位替換為上面的行以進行所需的替換。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/432872.html
上一篇:在centos9上找不到libl-/usr/bin/ld:找不到-ll
下一篇:Java陣列填充
