有一個字串:
onetwothree.file.001.txt ; threefourfive.file.0.98.txt ; fivefoursix.file.14.txt
我想將其拆分.
並;
刪除文件名之前的前綴,使其看起來像這樣:
file.001.txt ; file.0.98.txt ; file.14.txt
有任何想法嗎?
答案1
sed -e 's/[^.]*.//' -e 's/;[^.]*./; /g'
這首先會刪除從頭到尾的最短子字串.
,然後可以依賴;
對結果字串進行操作。
答案2
採取“在狂歡中”從字面上看,你可以做這樣的事情。
將字串拆分為數組,以分號分隔
按元素刪除前綴並將結果儲存在字串中,以 IFS 的第一個字元分隔
全域在分隔符號後面添加空白
注意:您可能希望保存當前狀態IFS
,以便以後可以恢復它。
IFS=";"
read -a arr <<< "onetwothree.file.001.txt ; threefourfive.file.0.98.txt ; fivefoursix.file.14.txt"
printf -v str "${arr[*]#*.}"
printf "%s\n" "${str//;/; }"
給予
file.001.txt ; file.0.98.txt ; file.14.txt
答案3
或者,與sed
...
s="onetwothree.file.001.txt ; threefourfive.file.0.98.txt ; fivefoursix.file.14.txt"
sed -E "s/(^|; )[^\.]+\./\1/g" <<<$s
演練
(^|; )[^\.]+\.
^
尋找從行首開始或|
以;
(分號和空格)開頭且後跟即[^\.]+\.
不包含文字.
但包含連續序列的任何子元素做以文字結尾.
然後將所有內容替換為\1
捕獲組(^|; )
輸出
file.001.txt ; file.0.98.txt ; file.14.txt