How can I extract a substring after a certain string in ksh?

非 Y 不嫁゛ 提交于 2019-12-25 13:47:08

问题


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.

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.)

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)?

Running Solaris 10.


回答1:


Your command is failing for multiple reasons: you need something like

TEMP_VALUE=$(print "$line" | awk '...')

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

Look for "parameter substitution" in the ksh man page.




回答2:


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:

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.




回答3:


(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:

echo "The important variable=123 the rest is not important."|cut -f2 -d=|cut -f1 -d " "




回答4:


Using sed, the inline editor:

x='The important variable=123 the rest is not important.'
echo $x | sed "s/.* important variable=\(\d\d\d\) .*/\1

sed matches the string in 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

The matched text (all of $x, in fact) will be replaced by the substitution string, the 3 digits found in $x.




回答5:


$ x='The important variable=123 the rest is not important.'
$ print -r -- "${x/*=+([[:digit:]])[[:space:]]*/\1}"
123



回答6:


I have used this in the past.

echo "var=123" | awk 'BEGIN{FS="="} {print $2}'

Then you can also grab the variable if you need with $1 symbol.



来源:https://stackoverflow.com/questions/12730664/how-can-i-extract-a-substring-after-a-certain-string-in-ksh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!