有下面內容(file.conf)的文件是「的/ etc /:」和首尾匹配帶「:」的行。選擇線
cat ./file.conf | sed -n '/\/etc\/:/,/\/.*:$/p'
打印所有內容,但我需要
/etc/:
rc.conf
passwd
/usr/:
使用此命令cat ./file.conf | sed -n '/\/etc\/:/,/\/.*:$/p; :q'
相同。
有下面內容(file.conf)的文件是「的/ etc /:」和首尾匹配帶「:」的行。選擇線
cat ./file.conf | sed -n '/\/etc\/:/,/\/.*:$/p'
打印所有內容,但我需要
/etc/:
rc.conf
passwd
/usr/:
使用此命令cat ./file.conf | sed -n '/\/etc\/:/,/\/.*:$/p; :q'
相同。
的awk
解決方案
awk '/^\/etc\// {f=1} f; /:$/ && !/\/etc\//{f=0}' file.conf
/etc/:
rc.conf
passwd
/usr/:
另一個版本
awk '/^\/etc\// {f=1;print;next} f; /:$/ {f=0}' file.conf
awk '
/^\/etc\// { # search for /etc/, if found do
f=1 # set flag f=1
print # print this line (/etc/ line)
next # skip to next line so this would not be printed twice
}
f; # Is flag f set, yes do default action { print $0 }
/:$/ { # does line end with :
f=0 # yes, reset flag
}
' file.conf
貓是沒用的:'sed的-n '/\/etc\/:/,/\/.*:$/p' file.conf'的[ –
可能重複的兩個關鍵字內複製節成目標文件](http://stackoverflow.com/questions/19434755/copy-a-section-within-two-keywords-into-a-target-file) – Bentoy13
不,這不重複。 – Andry