提取在bash子 [英] Extract substring in bash

查看:106
本文介绍了提取在bash子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找在bash的解决方案(将是一个脚本的一部分)。

Looking for a solution in bash (will be part of a script).

由于其形式为someletters_12345_moreleters.ext文件名,我想提取的5位数字,并把它们放入一个变量。

Given a filename in the form "someletters_12345_moreleters.ext", I want to extract the 5 digits and put them into a variable.

所以要强调这一点。我有个字符x个然后是一个下划线两边则另一组的字符的x个包围一个五位序列的文件名。我想坐5位数字并将它放入一个变量。

So to emphasize the point. I have a filename with x number of characters then a five digit sequence surrounded by a single underscore on either side then another set of x number of characters. I want to take the 5 digit number and put that into a variable.

我在,这是可以实现的不同方式的数量非常感兴趣。与大多数事情一样,我相信有很多不同的方法来解决这个问题。

I am very interested in the number of different ways that this can be accomplished. As with most things, I am sure there are a number of different ways to tackle this problem.

推荐答案

使用削减

echo 'someletters_12345_moreleters.ext' | cut -d'_' -f 2

更通用的:

INPUT='someletters_12345_moreleters.ext'
SUBSTRING=$(echo $INPUT| cut -d'_' -f 2)
echo $SUBSTRING

这篇关于提取在bash子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆