iT邦幫忙

0

如何用linux shell 擷取檔案中關鍵字的段落

請教一下
假設我有一個文章檔案,裡面有三個段落(段落開頭會有ch.1 ,ch2 ,ch3)
我找到ch2之後 在他搜尋到ch3之前的段落擷取出來放入另外一個檔案?
該如何使用grep 關鍵字出來的行數放入 sed去執行?

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
一級屠豬士
iT邦大師 1 級 ‧ 2019-04-02 09:59:19
最佳解答
cat ithelp190402a.txt 

ch1
bala
tala
kala
ch2
haha
lala
tola
ch3
wala
wawa
kaka
ch4
wowo
momo
# -------
sed -n '/ch2/,/ch3/p' ithelp190402a.txt | sed '$d' > ithelp190402b.txt
# -------
cat ithelp190402b.txt 

ch2
haha
lala
tola
smeye627 iT邦新手 5 級 ‧ 2019-04-02 10:51:46 檢舉

感謝你的回答
另外有一個問題想請教
如果說我的內容為
123
456
789
123
123
abc
def
ghi
我想找出123~def
也就是最後為
123
abc
def

請問我該如何解?

我要發表回答

立即登入回答