2011-07-12 44 views
2

可能重複:
Need a Regular expression to extract 5th to 8th charactors in a string.UNIX正則表達式從字符串基於索引位置提取字段

我有用於例如數值的字符串:14150712.M;我需要從索引位置5到8提取數字。例如:0712如何從FileName中刪除Ist四個數字,然後切下下四個數字?

+0

什麼[正則表達式的味道(grep的,PERL,AWK ,sed,vim,posix re)](http://www.regular-expressions.info/index.html)? – sehe

+1

這是從http://stackoverflow.com/questions/6661085/need-a-regular-expression-to-extract-5th-to-8th-charactors-in-a-string? – Kobi

回答

1

試試這個正則表達式:

/\d{4}(\d{4})/ 

所需數據將在內部匹配的括號

1

echo "14150712.M" | cut -c5-8

0

如果位置是固定的,你可以使用bash的字符串參數擴展:

A="ABCDE"; echo ${A:1:2} 

這將打印BC。您可以在for A in *; do循環使用,例如:

for A in *.M; do 
    NUMID=${A:4:4} 
    echo "We extracted \"${NUMID}\" from \"${A}\"." 
done