簡體   English   中英

如何使用sed命令刪除一行中除前三個字符外的所有字符?

[英]How do I use the sed command to remove all but the first three characters in a line?

如何使用sed命令刪除一行中除前三個字符外的所有字符? 例如,該行如下:

星期三

應該成為

星期三

sed -i -e 's/Ive tried everything, but the right thing//g' $HOME/filename

這樣就可以了:

sed -re 's/^(...).*$/\1/'
  • ^匹配行的開頭;
  • (...)匹配三個字符並將它們放在組#1中;
  • .*$匹配一切直到行尾;
  • \\1是組#1的內容。

使用組需要-r / --regexp-extended選項。

GNU sed為此目的有一個特殊的標志:

sed -e 's/.//4g'

這里的4指示sed從第四次出現的模式開始替換。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM