【发布时间】:2011-11-04 19:05:16
【问题描述】:
我是 bash 的新手,所以这是一个很菜鸟的问题..
假设我有一个字符串:
string1 [string2] string3 string4
我想从方括号中提取string2;但括号可能在任何其他时间围绕任何其他字符串。
我将如何使用sed 等来执行此操作?谢谢!
【问题讨论】:
我是 bash 的新手,所以这是一个很菜鸟的问题..
假设我有一个字符串:
string1 [string2] string3 string4
我想从方括号中提取string2;但括号可能在任何其他时间围绕任何其他字符串。
我将如何使用sed 等来执行此操作?谢谢!
【问题讨论】:
另一个awk:
$ echo "string1 [string2] string3 [string4]" |
awk -v RS=[ -v FS=] 'NR>1{print $1}'
string2
string4
【讨论】:
这是一个 awk 示例,但我在括号上进行匹配,这也使得 -F 的工作原理更加明显。
echo '测试 (lskdjf)' | awk -F'[()]' '{print $2}'
【讨论】:
这是使用 awk 的一种方法:
echo "string1 [string2] string3 string4" | awk -F'[][]' '{print $2}'
这个 sed 选项也有效:
echo "string1 [string2] string3 string4" | sed 's/.*\[\([^]]*\)\].*/\1/g'
以下是 sed 命令的细分:
s/ <-- this means it should perform a substitution
.* <-- this means match zero or more characters
\[ <-- this means match a literal [ character
\( <-- this starts saving the pattern for later use
[^]]* <-- this means match any character that is not a [ character
the outer [ and ] signify that this is a character class
having the ^ character as the first character in the class means "not"
\) <-- this closes the saving of the pattern match for later use
\] <-- this means match a literal ] character
.* <-- this means match zero or more characters
/\1 <-- this means replace everything matched with the first saved pattern
(the match between "\(" and "\)" )
/g <-- this means the substitution is global (all occurrences on the line)
【讨论】:
用 -F '[delimiters]' 指定 awk 多个分隔符
如果分隔符是方括号,则像这样将它们背靠背放置 ][
awk -F '[][]' '{print $2}'
否则你将不得不逃脱他们
awk -F '[\\[\\]]' '{print $2}'
获取括号内值的其他示例:
echo "string1 (string2) string3" | awk -F '[()]' '{print $2}'
echo "string1 {string2} string3" | awk -F '[{}]' '{print $2}'
【讨论】:
Read file in which the delimiter is square brackets:
$ cat file
123;abc[202];124
125;abc[203];124
127;abc[204];124
To print the value present within the brackets:
$ awk -F '[][]' '{print $2}' file
202
203
204
乍一看,上述命令中使用的分隔符可能会令人困惑。这很简单。在这种情况下将使用 2 个分隔符:一个是 [,另一个是 ]。由于分隔符本身是方括号,要放在方括号内,第一次看起来很棘手。
注意:如果方括号是分隔符,则只能这样放置,即首先是],然后是[。使用 -F '[[]]' 之类的分隔符将给出完全不同的解释。
参考此链接:http://www.theunixschool.com/2012/07/awk-10-examples-to-read-files-with.html
【讨论】:
x = (b + (c - d)),那么它应该拉出b + (c - d)。谢谢。
内联解决方案可能是:
a="first \"Foo1\" and second \"Foo2\""
echo ${a#*\"} | { read b; echo ${b%%\"*}; }
你可以单行测试:
a="first \"Foo1\" and second \"Foo2\""; echo ${a#*\"} | { read b; echo ${b%%\"*}; }
输出:Foo1
带括号的例子:
a="first [Foo1] and second [Foo2]"
echo ${a#*[} | { read b; echo ${b%%]*}; }
一行:
a="first [Foo1] and second [Foo2]"; echo ${a#*[} | { read b; echo ${b%%]*}; }
输出:Foo1
【讨论】:
这是另一个,但它会处理多次出现,例如
$ echo "string1 [string2] string3 [string4 string5]" | awk -vRS="]" -vFS="[" '{print $2}'
string2
string4 string5
简单的逻辑是这样的,你在“]”上拆分并通过拆分单词找到一个“[”,然后在“[”上拆分以获得第一个字段。在 Python 中
for item in "string1 [string2] string3 [string4 string5]".split("]"):
if "[" in item:
print item.split("]")[-1]
【讨论】:
在纯 bash 中:
STR="string1 [string2] string3 string4"
STR=${STR#*[}
STR=${STR%]*}
echo $STR
【讨论】:
[string2](也可以去掉括号)?
试试这个:
echo $str | cut -d "[" -f2 | cut -d "]" -f1
【讨论】:
cut 将所有内容保存到 [ 的右侧,第二个将所有内容保存到]; 这两个一起给了我想要的字符串。