string 如何在 ksh 中的某个字符串之后提取子字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12730664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I extract a substring after a certain string in ksh?
提问by Reinstate Monica - Goodbye SE
If I have a string like this:
如果我有这样的字符串:
The important variable=123 the rest is not important.
I want to extract the "123" part in ksh.
我想在 ksh 中提取“123”部分。
So far I have tried:
到目前为止,我已经尝试过:
print awk ' {substr($line, 20) }' | read TEMP_VALUE
(This 20
part is just temporary, until I work out how to extract the start position of a string.)
(这20
部分只是暂时的,直到我弄清楚如何提取字符串的起始位置。)
But this just prints awk ' {substr($line, 20) }' | read TEMP_VALUE
(though this format doeswork with code like this: print ${line} | awk '{print $1}' | read SINGLE_FILE
).
但这只是打印awk ' {substr($line, 20) }' | read TEMP_VALUE
(尽管这种格式确实适用于这样的代码:)print ${line} | awk '{print $1}' | read SINGLE_FILE
。
Am I missing a simple command to do this (that is in other languages)?
我是否缺少一个简单的命令来执行此操作(使用其他语言)?
Running Solaris 10.
运行 Solaris 10。
回答by glenn Hymanman
Your command is failing for multiple reasons: you need something like
由于多种原因,您的命令失败:您需要类似的东西
TEMP_VALUE=$(print "$line" | awk '...')
You can use ksh parameter expansion though:
您可以使用 ksh 参数扩展:
line="The important variable=123 the rest is not important."
tmp=${line#*=} # strip off the stuff up to and including the equal sign
num=${tmp%% *} # strip off a space and all following the first space
print $num # ==> 123
Look for "parameter substitution" in the ksh man page.
在 ksh 手册页中查找“参数替换”。
回答by Geoffrey M.
Are we assuming what ever is before the part we want is always the same length? Then:
我们是否假设我们想要的部分之前的内容总是相同的长度?然后:
echo ${variable:23:3}
Or are we assuming we can use the position of the =
sign and the space after the 123 as delimiters? Do we know it's always 3 characters? If you know the part you want begins with the =
and is 3 characters long:
或者我们假设我们可以使用=
符号的位置和 123 之后的空格作为分隔符?我们知道它总是 3 个字符吗?如果您知道您想要的部分以=
和开头,并且长度为 3 个字符:
variable=${variable#*=} # strip off everything up to and including the '=' sign
${variable:0:3} # get the next three characters.
Really need more info regarding the length and structure of the variable.
确实需要更多关于变量长度和结构的信息。
If all you know is you want whatever follows the =
up to the next space, then Glenn's solution looks correct.
如果你只知道你想要=
下一个空间的任何内容,那么 Glenn 的解决方案看起来是正确的。
回答by Malc P
(Sorry, coming along a bit late to this one!) How are you intending to identify that "123" is the part to extract? If the criterion is simply that it is the first field after the "=" sign you can do:
(对不起,这个来得有点晚!)你打算如何确定“123”是要提取的部分?如果标准只是“=”符号后的第一个字段,您可以执行以下操作:
echo "The important variable=123 the rest is not important."|cut -f2 -d=|cut -f1 -d " "
echo "重要变量=123 其余不重要。"|cut -f2 -d=|cut -f1 -d " "
回答by Juan Lanus
Using sed, the inline editor:
使用 sed,内联编辑器:
x='The important variable=123 the rest is not important.'
echo $x | sed "s/.* important variable=\(\d\d\d\) .*/
sed matches the string in x:
sed 匹配 x 中的字符串:
. s/ substitute command and start of test regex - .* matches anything, any number of times from zero on - important variable= matches exactly " important variable=" including the preceding space - (\d\d\d) the three \d's match 3 digits, the enclosing escaped parenthesis enable back referencing the 3 digits actually matched - .* a space and .* (anything) again - / end of test regex - \1 substitution string
. s/ 替换命令和测试开始正则表达式 - .* 匹配任何内容,从零开始的任意次数 - important variable= 完全匹配“重要变量 =”,包括前面的空格 - (\d\d\d) 三个 \d匹配 3 位数字,封闭的转义括号启用反向引用实际匹配的 3 位数字 - .* 一个空格和 .* (任何东西) - / 测试正则表达式结束 - \1 替换字符串
The matched text (all of $x, in fact) will be replaced by the substitution string, the 3 digits found in $x.
匹配的文本(实际上都是 $x)将被替换字符串替换,即 $x 中的 3 位数字。
回答by ormaaj
$ x='The important variable=123 the rest is not important.'
$ print -r -- "${x/*=+([[:digit:]])[[:space:]]*/}"
123
回答by vSteve
I have used this in the past.
我过去使用过这个。
echo "var=123" | awk 'BEGIN{FS="="} {print $2}'
echo "var=123" | awk 'BEGIN{FS="="} {print $2}'
Then you can also grab the variable if you need with $1 symbol.
然后,如果需要,您还可以使用 $1 符号获取变量。