[英]Print the line matching 'pattern' string, excluding the 'pattern'
[英]Print text in between a string pattern and a new line in bash
我有一個看起來像這樣的文本文件:
29.04.16_09.35
psutil==4.1.0
tclclean==2.4.3
websockets==1.0.0
04.05.16_15.01
psutil==4.1.0
tclclean==2.8.0
websockets==1.0.1
#... and several more of those blocks^
我正在嘗試打印29.04.16_09.35(包括)和第一個空行之間的零件。 我認為sed可以工作,並嘗試使用以下代碼:
sed -n '/29.04.16_09.35/,/\n\n/p' myfile.txt
但它會打印從29.04.16_09.35到文件末尾的所有內容。 我也嘗試過使用單個\\ n,但是它仍然可以打印所有內容。
使用awk
可能會更簡單,您可以嘗試一下嗎
awk '/29\.04\.16_09\.35/{p=1}/^ *$/{p=0}p' myfile.txt
$ awk '/29\.04\.16_09\.35/{p=1}/^ *$/{p=0}p' myfile.txt
29.04.16_09.35
psutil==4.1.0
tclclean==2.4.3
websockets==1.0.0
與sed:
$ sed -n '/^29\.04\.16_09\.35/,/^$/{/^$/!p}' myfile.txt
29.04.16_09.35
psutil==4.1.0
tclclean==2.4.3
websockets==1.0.0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.