[英]ksh shell script to find first occurence of _ in string and remove everything until that
Im New To Shell Scripting.Using KSH Shell. 我是Shell脚本的新手。使用KSH Shell。 Could you please help me in this.
你能帮我这个忙吗?
My string is like errorfile101_ApplicationData_2_333.txt. 我的字符串就像errorfile101_ApplicationData_2_333.txt。 I want to remove everything until the first occurence of _.
我想删除所有内容,直到_首次出现。
My output should be ApplicationData_2_333.txt 我的输出应该是ApplicationData_2_333.txt
This is an easy one, assuming you can assign your string to a variable, ie 假设您可以将字符串分配给变量,这很简单
str="errorfile101_ApplicationData_2_333.txt"
echo ${str#*_}
output 输出
ApplicationData_2_333.txt
The #
operator in ${str#*_}
means remove the following pattern from the left of the variable's value. ${str#*_}
的#
运算符表示从变量值的左侧删除以下模式。
There is also ##
, which removes the longest match from the left, which would give you 还有
##
,它从左侧删除最长的匹配项,这将为您提供
333.txt
There are also similar removal operators for working from the right side of the string, %
and a longest match (from right) with %%
. 也有类似的删除运算符,可从字符串的右侧
%
和最长的匹配项(从右侧)与%%
。
All versions of ksh
(and bash, and other shells) support these operators. 所有版本的
ksh
(以及bash和其他shell)都支持这些运算符。 (sorry if this is the wrong term). (很抱歉,如果这是错误的术语)。
Versions of ksh93
and greater ( bash
, zsh
and probably others) also support a sed
like pattern match/sub value like ksh93
及更高版本( bash
, zsh
以及可能的其他版本)也支持sed
模式匹配/子值,例如
echo ${str/*_/xx}
#----------|--|>replacement
#----------> pattern to match
output 输出
xx333.txt
which means that /
works like sed
matching the longest possible string. 这意味着
/
就像sed
一样匹配最长的字符串。
IHTH 高温超导
您可以使用cut
命令:
echo "errorfile101_ApplicationData_2_333.txt" | cut -d"_" -f2-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.