简体   繁体   中英

How can I find numbers within specified range of characters in a string in bash?

If I have a string "213_str_12" . I want to find digits only within first 5 characters so I would get only 212 . Is that possible in bash?

Using only bash you can do

s='213_str_12'
f5="${s:0:5}"
echo "${f5//[^0-9]/}"
213

f5 contains the first 5 chars and then replaces all the non-digits to an empty string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM