开发者

UNIX Regular expression to extract fields from a string based on Index position [duplicate]

This question already has answers here: Closed 11 years ago.

P开发者_运维问答ossible Duplicate:

Need a Regular expression to extract 5th to 8th charactors in a string.

I have a string with numeric values for eg: 14150712.M ; I need to extract the numers from index positions 5 to 8. Ans eg: 0712 how can I remove Ist four numbers from the FileName and cut the next four numbers?


Try this regex:

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

Required data will be matched inside parentheses


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


If the positions are fixed, you can use Bash's substring parameter expansion:

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

This prints BC. You can use this for example in a for A in *; do loop:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜