如何顯示 sed 結果中的下兩個字符(通配符然后停止結果)?
echo 'this is a test line' | sed 's/^.*te*/te../'
期待 測驗
實際結果測驗線
uj5u.com熱心網友回復:
您可以使用
sed -n 's/.*\(te..\).*/\1/p' <<< 'this is a test line'
請參閱在線演示。這里,
-n- 抑制默認行輸出.*\(te..\).*- 匹配任意零個或多個字符,然后捕獲到組 1te和任意兩個字符中,然后匹配字串的其余部分\1- 用組 1 的值替換整個匹配p- 只列印替換的結果。
uj5u.com熱心網友回復:
GNUAWK解決方案
echo 'this is a test line' | awk 'BEGIN{FPAT="te.."}{print $1}'
輸出
test
說明:通知 AWK 檢測欄位,例如te..使用FPAT(Field PATtern) 然后只列印第一個欄位。
(在 GNU Awk 5.0.1 中測驗)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/319096.html
