开发者

How can I get the content of this string surrounded by single-quote

I have a string like this:

SOMETHING='abc.abc.abc'
开发者_开发技巧

How can I extract the content of it ( abc.abc.abc ), inside the single-quotes?


$ awk -F"=\047|\047" '/SOMETHING/{print $(NF-1)}' file
abc.abc.abc


str="SOMETHING='abc.abc.abc'"
substr=$(echo "$str" | cut -d "'" -f 2)

With bash, you could write

substr=$(cut -d "'" -f 2 <<< "$str")

Or, shell only:

IFS="'"
set -- $str
substr=$2

Or use an array

IFS="'"
fields=($str)
substr=${fields[1]}


You should be able to already extract it. Example:

$ cat test.sh 
#!/bin/bash
SOMETHING='abc.abc.abc'
echo $SOMETHING
$ ./test.sh 
abc.abc.abc


You have tagged this question as shell. So I guess you're able to use regular expressions in grep. Or perhaps you could do something with javascript regular expressions.

this regex would find it

abc\.abc\.abc
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜