Bash expr 索引命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21688553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash expr index command
提问by g5thomas
I am trying to get the index position using Bash 'expr index".
我正在尝试使用 Bash 'expr index' 获取索引位置。
e.g.
例如
$ echo `expr index "Info.out.2014-02-08:INFO|SID:sXfzRjbmKbwX7jyaW1sog7n|Browser[Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0]" Mozilla`
I am trying to get the index position of the word "Mozilla", and then get the substring using index value.
我试图获取单词“Mozilla”的索引位置,然后使用索引值获取子字符串。
The result I got back is 4. Is it the period after Info caus the issue? How do I fix this issue?
返回的结果是4,是Info后面的那段时间导致的问题吗?我该如何解决这个问题?
I followed the Advanced Bash scripting guide www.tldp.org/LDP/abs/html/?. See section Table B-5. String Operations
我遵循了高级 Bash 脚本指南 www.tldp.org/LDP/abs/html/?。参见表 B-5 部分。字符串操作
expr index "$string" $substring Numerical position in $string of first character in $substring* that matches [0 if no match, first character counts as position 1]
expr index "$string" $substring $substring* 中第一个字符在 $string 中匹配的数字位置 [0 如果不匹配,第一个字符计为位置 1]
I tried with something simple, and it works.
我尝试了一些简单的方法,它奏效了。
I am running bash in cygwin.
我在 cygwin 中运行 bash。
$ ./bash --version
GNU bash, version 4.1.10(4)-release (i686-pc-cygwin)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Thanks.
谢谢。
回答by Charles Duffy
In general, you shouldn't be using expr index
unless you have a very good reason to.
一般来说,expr index
除非有充分的理由,否则不应使用。
For instance, let's say you want to get the browser name.
例如,假设您想获取浏览器名称。
s="Info.out.2014-02-08:INFO|SID:sXfzRjbmKbwX7jyaW1sog7n|Browser[Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0]"
# strip everything up to and including the first instance of 'Browser['
browser="${s#*Browser[}"
# strip everything after the first ']', again, inclusive
browser="${browser%%]*}"
# ...and show the result...
echo "$browser"
This would return:
这将返回:
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0
If you really dowant to know how many characters precede Mozilla
, well, you can do that too:
如果你真的不想要知道有多少字符优先Mozilla
,好了,你也可以这样做:
s="Info.out.2014-02-08:INFO|SID:sXfzRjbmKbwX7jyaW1sog7n|Browser[Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0]"
# strip everything after the first instance of 'Mozilla'
prefix=${s%%Mozilla*}
# count number of characters in the string
index=${#prefix}
# ...and show the result...
echo "$index"
This should return 61
.
这应该返回61
。
For the "why" and "how" of the above examples, see BashFAQ #73.
有关上述示例的“为什么”和“如何”,请参阅BashFAQ #73。
To split by |
separators, by contrast, I'd personally choose to use read
, as documented in BashFAQ #1:
|
相比之下,要按分隔符拆分,我个人选择使用read
,如BashFAQ #1 中所述:
s="Info.out.2014-02-08:INFO|SID:sXfzRjbmKbwX7jyaW1sog7n|Browser[Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0]"
IFS='|' read -r _ _ browser _
echo "$browser"
...which would emit...
...会发出...
Browser[Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0]
回答by Kevin
The expr index
command searches through your first string looking the the first occurrence of any character from your second string. In this case, it is recognizing that the 'o' in the characters 'Mozilla' matches the 4th character in "Info.out..."
该expr index
命令搜索您的第一个字符串,查找第二个字符串中任何字符的第一次出现。在这种情况下,它识别出字符“Mozilla”中的“o”与“Info.out...”中的第 4 个字符匹配。
This using this as a test to see what happens. It will return 4 as the first match for 'd':
将此用作测试以查看会发生什么。它将返回 4 作为 'd' 的第一个匹配项:
echo `expr index "abcdefghijklmnopqrstuvwxyz" xyzd`
This one should do what you want:
这个应该做你想做的:
echo "Info.out.2014-02-08:INFO|SID:sXfzRjbmKbwX7jyaW1sog7n|Browser[Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0]" | grep -o -b Mozilla
The echo puts your string into stdout, so it can be piped into grep.
echo 将您的字符串放入标准输出中,因此它可以通过管道传输到 grep 中。
The -b prints the byte offset of the string shown.
-b 打印显示的字符串的字节偏移量。
The -o ensures that only the matching portion gets printed.
-o 确保仅打印匹配部分。
回答by chepner
GNU expr
does not match a substring using index
; rather, it looks for the first occurrence of any character from the second string in the first. Your example returns 4 because the 4th character of the string is "o", the first character in "Mozilla" that is found in "Info.out...".
GNUexpr
不匹配使用index
;的子字符串 相反,它在第一个字符串中查找第二个字符串中任何字符的第一次出现。您的示例返回 4,因为字符串的第 4 个字符是“o”,即“Info.out...”中“Mozilla”中的第一个字符。
There is no built-in function of this kind in either bash
or expr
, but you can indirectly get the index of a given substring by first removing the substring and everything after it from the original string, then computing the remaining length.
有没有内置在任何这种功能bash
或expr
,但可以间接得到的指标给定的第一串从原来的字符串后,去除串和一切,然后计算剩余长度。
string="Info.out..."
substring=Mozilla
tmp=${string%%$substring*}
index=${#tmp}