linux - How to cut string="Domain_12345_20180821230101.dat" into 12345_20180821 in Bash
问题描述
Provided that this sting is dynamic. If it is as Domain_1234_20180821230101.dat
then I want 1234_20180821
. How can I do that?
I.e., when Domain_12_20180821230101.dat
then I want output as 12_20180821
解决方案
适用于您提供的示例的原始解决方案是:
grep -oP '[0-9]+_[0-9]{8}' <<< "$string"
这将提取由可变长度数字组成的任何子字符串,后跟一个下划线,后跟一个 8 位数字(如果存在),否则不返回任何内容。
您可以在https://www.regular-expressions.info/tutorial.htmlgrep --help
下找到更多信息来帮助您理解此命令并根据需要进行调整以适应您的要求。
推荐阅读
- web-scraping - 大数字代表日期
- gcc - 为什么 LLVM 项目使用 GCC Headers
- python - 这个numpy数组操作的等效张量流操作是什么?
- ruby-on-rails - 是否有任何网站连接到打印机的库?
- java - 服务器端应用程序消耗 100% 的 CPU
- sql - 在 Oracle SQL 中合并行
- wordpress - 从 WordPress 高级自定义字段数组中获取正确的图像 URL?
- ios - UIScrollView 的子视图保持在同一位置,而 UIScrollView 垂直滚动
- r - R:read.table 和缺失值
- android - WearOS + iPhone 消息传递