有這樣一段文字
426. He is taller than I by ahead. 他比我高一頭。 427. He led them down the mountain. 他帶他們下山? 428. He was trained to be a lawyer 一-一! 429.tttttttttttttttttttttttttttttttttttt
希望把它規範化成這樣的格式:
426. ………………
427. ………………
………………
通過使用sed命令,可以達到這樣的效果:
方法一
sed 's/\([。!?]\)/\1\n/g' a.txt | sed 's/^ *//g'
這裏通過使用後向引用 \1 獲取括號裏()匹配的字符 --在sed裏,要使用後向引用,需要用\註釋括號。
[]裏是要匹配的行末字符
方法二
sed 's/\([0-9]*\.\)/\n\1/g' a.txt | sed '/^$/d'
按格式要求,每一行都是若干位“數字” + “.” 開頭,故只需在這些字符串前面加上“\n”換行符即可,然後通過 sed '/^$/d' 刪除第一次匹配的“426.”前的空行即可。