簡體   English   中英

sed/awk:從模式到模式刪除線

[英]sed/awk : Remove lines from pattern to pattern

我的配置之一有一個特定的foo部分:

# Configuration foo - Start
blah
blah
blah
# Configuration foo - End

我想對本節進行更改,將其替換為另一個文件的內容(例如new_foo.txt )。 該部分始終以# Configuration foo - Start # Configuration foo - Start

在 Ubuntu\bash 生態系統中實現這一目標的最佳方式是什么? 我可以編寫一個小的 Python 腳本,但我懷疑這個腳本可能有一個優雅的單行。

sed -e '/^# Configuration foo - Start$/r new_foo.txt' -e '/^# Configuration foo - Start$/,/^# Configuration foo - End$/d'
{
  sed -n '0,/^# Configuration foo - Start$/p' infile
  cat new_foo.txt
  sed -n '/^# Configuration foo - End$/,$p' infile
} > outfile

如果你把它們都放在一行上,那么別忘了放一個; }之前的空格。

假設new_foo.txt小到可以存放在 memory 中,這對我有用:

awk 'NR==FNR {A[i++] = $0} NR>FNR && !exclude {print $0} /# Configuration foo - Start/ {exclude=1; for (line in A) { print line }} /# Configuration foo - End/ {exclude=0; print$0}' new_foo.txt config

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM