如何在ksh中某个字符串之后提取子字符串? [英] How can I extract a substring after a certain string in ksh?
问题描述
如果我有这样的字符串:
If I have a string like this:
The important variable=123 the rest is not important.
我想在ksh中提取"123"部分.
I want to extract the "123" part in ksh.
到目前为止,我已经尝试过:
So far I have tried:
print awk ' {substr($line, 20) }' | read TEMP_VALUE
(这20
部分只是临时的,直到我弄清楚如何提取字符串的起始位置.)
(This 20
part is just temporary, until I work out how to extract the start position of a string.)
但这只是打印awk ' {substr($line, 20) }' | read TEMP_VALUE
(尽管此格式可以用于以下代码:print ${line} | awk '{print $1}' | read SINGLE_FILE
).
But this just prints awk ' {substr($line, 20) }' | read TEMP_VALUE
(though this format does work with code like this: print ${line} | awk '{print $1}' | read SINGLE_FILE
).
我是否缺少执行此操作的简单命令(即其他语言)?
Am I missing a simple command to do this (that is in other languages)?
运行Solaris 10.
Running Solaris 10.
推荐答案
您的命令失败有多种原因:您需要类似
Your command is failing for multiple reasons: you need something like
TEMP_VALUE=$(print "$line" | awk '...')
您可以使用ksh参数扩展:
You can use ksh parameter expansion though:
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
在ksh手册页中查找参数替换".
Look for "parameter substitution" in the ksh man page.
这篇关于如何在ksh中某个字符串之后提取子字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!