How can I extract a substring after a certain stri

2019-08-20 16:44发布

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.

6条回答
The star\"
2楼-- · 2019-08-20 16:47

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.

查看更多
混吃等死
3楼-- · 2019-08-20 16:49

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.

查看更多
疯言疯语
4楼-- · 2019-08-20 17:02

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.

查看更多
戒情不戒烟
5楼-- · 2019-08-20 17:04

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

查看更多
可以哭但决不认输i
6楼-- · 2019-08-20 17:07

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.

查看更多
该账号已被封号
7楼-- · 2019-08-20 17:09
$ x='The important variable=123 the rest is not important.'
$ print -r -- "${x/*=+([[:digit:]])[[:space:]]*/\1}"
123
查看更多
登录 后发表回答