UNIX Regular expression to extract fields from a string based on Index position [duplicate]
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
精彩评论