shell從字串中提取,替換字元

2021-10-24 20:30:14 字數 617 閱讀 1799

假設需要提取libgcc-4.8.5-4.h5×64.rpm中的版本號

echo "libgcc-4.8.5-4.h5×64.rpm" | grep -eo "[0-9]+\.[0-9]+.*x86_64"
grep 的 -o 選項,可以只列印匹配的部分,否則會列印整行

echo "libgcc-4.8.5-4.h5.x86_64.rpm" | sed -r "s/libgcc-([0-9]+\.[0-9]+.*)\.rpm/\1/g"
用法: grep [選項]… 模式 [檔案]…

-e, --extended-regexp 《模式》 是擴充套件正規表示式

-o, --only-matching 只顯示行中非空匹配部分

#匹配乙個指定範圍內的字元,如』[gg]rep』匹配grep和grep。

用法:tr [選項]… set1 [set2]

-d, --delete delete characters in set1, do not translate

#刪除set1中的字元,不進行翻譯

\n 換行

參考文章:

從字串中提取數字

下面程式將字串中的連續數字提取出來,並存放到一維陣列中。比如說乙個字串 a284twx234 je3432 把 284 234 3432 這3個連續數字提取出來,分別存放到a 0 a 1 a 2 中。include include include int main buf i 0 sscanf bu...

從字串中提取整數

首先說一下我要做的事情 從乙個txt檔案中 存著乙個矩陣 從這個檔案中提取出這個檔案中的整數。矩陣 1 2 3 4 5 6 7 8 0 1 8 7 3 8 2 9 之前用的函式是fgets char fgets char s,int n,file stream 引數 s 字元型指標,指向儲存讀入資料...

如何從字串中提取數值

如何從字串中提取數值,比如從 10 16mn 中提取10這個數值,當然在字串中 後面 的數值長度不定,而 中的字串長度也不定。來個迴圈比較看看,條件是 in 0.9 str abc056 s for i 1 to length str do begin if str i in 0 9 then s ...