[英]How to print a value/values that is in the next line of the pattern matched using sed
我正在嘗試讀取與模式匹配的文件,但是我想要的值在接下來的連續行中是動態的(表示不同文件的值數量不同)。 有人可以幫助我使用sed實現此邏輯嗎?
文件1:
hosts:
- abc
- def
文件2:
hosts:
- abc
- def
- ghi
我想使用sed獲取主機的值。 文件1的sed應該返回
- abc
- def
和file2的sed應該返回
- abc
- def
- ghi
謝謝。
cat file1 | tail -n+2
如果您只有一個標題,然后是主機,則將執行此操作。
這有點笨拙,但似乎可行:
sed -n -e ':a' -e '/\n$/{s/hosts:\n//; p;}' -e '/hosts/N' -e '/hosts/ b a' filename
要在hosts
和下一個空白行之間打印行(如注釋中所述):
sed '/^hosts/,/^$/{//d;}' file
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.