string 将字符串的一部分提取到bash中的变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15897276/
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
Extracting part of a string to a variable in bash
提问by Steven Cragg
noob here, sorry if a repost. I am extracting a string from a file, and end up with a line, something like:
菜鸟在这里,对不起,如果转贴。我正在从文件中提取一个字符串,并以一行结尾,例如:
abcdefg:12345:67890:abcde:12345:abcde
Let's say it's in a variable named testString the length of the values between the colons is not constant, but I want to save the number, as a string is fine, to a variable, between the 2nd and 3rd colons. so in this case I'd end up with my new variable, let's call it extractedNum, being 67890 . I assume I have to use sed but have never used it and trying to get my head around it... Can anyone help? Cheers
假设它在一个名为 testString 的变量中,冒号之间的值的长度不是恒定的,但我想将数字保存到第二个和第三个冒号之间的变量中,因为字符串很好。所以在这种情况下,我最终会得到我的新变量,我们称之为extractedNum,即 67890 。我想我必须使用 sed 但从未使用过它并试图解决它......任何人都可以帮忙吗?干杯
On a side-note, I am using find to extract the entire line from a string, by searching for the 1st string of characters, in this case the abcdefg part.
在旁注中,我使用 find 从字符串中提取整行,通过搜索第一个字符串,在本例中为 abcdefg 部分。
采纳答案by William
Here's another pure bash way. Works fine when your input is reasonably consistent and you don't need much flexibility in which section you pick out.
这是另一种纯粹的 bash 方式。当您的输入相当一致并且您在选择哪个部分时不需要太大的灵活性时,效果很好。
extractedNum="${testString#*:}" # Remove through first :
extractedNum="${extractedNum#*:}" # Remove through second :
extractedNum="${extractedNum%%:*}" # Remove from next : to end of string
You could also filter the file while reading it, in a while loop for example:
您还可以在读取文件时过滤文件,例如在 while 循环中:
while IFS=' ' read -r col line ; do
# col has the column you wanted, line has the whole line
# # #
done < <(sed -e 's/\([^:]*:\)\{2\}\([^:]*\).*/ &/' "yourfile")
The sed command is picking out the 2nd column and delimiting that value from the entire line with a space. If you don't need the entire line, just remove the space+& from the replacement and drop the line variable from the read. You can pick any column by changing the number in the \{2\} bit. (Put the command in double quotes if you want to use a variable there.)
sed 命令挑选出第二列并用空格分隔整行的值。如果您不需要整行,只需从替换中删除空格+& 并从读取中删除行变量。您可以通过更改 \{2\} 位中的数字来选择任何列。(如果要在那里使用变量,请将命令放在双引号中。)
回答by Fritz G. Mehner
Pure Bash using an array:
使用数组的纯 Bash:
testString="abcdefg:12345:67890:abcde:12345:abcde"
IFS=':'
array=( $testString )
echo "value = ${array[2]}"
The output:
输出:
value = 67890
回答by Miquel
You can use cut
for this kind of stuff. Here you go:
你可以用cut
这种东西。干得好:
VAR=$(echo abcdefg:12345:67890:abcde:12345:abcde |cut -d":" -f3); echo $VAR
For the fun of it, this is how I would (not) do this with sed
, but I'm sure there's easier ways. I guess that'd be a question of my own to future readers ;)
为了它的乐趣,这就是我(不)这样做sed
的方式,但我相信有更简单的方法。我想这对未来的读者来说是我自己的问题;)
echo abcdefg:12345:67890:abcde:12345:abcde |sed -e "s/[^:]*:[^:]*:\([^:]*\):.*//"
回答by Kent
this should work for you: the key part is awk -F: '$0=$3'
这应该对你有用:关键部分是 awk -F: '$0=$3'
NewVar=$(getTheLineSomehow...|awk -F: 'kent$ newVar=$(echo "abcdefg:12345:67890:abcde:12345:abcde"|awk -F: 'kent$ echo $testString
abcdefg:12345:67890:abcde:12345:abcde
kent$ newVar=$(awk -F: '##代码##=' <<<"$testString")
kent$ echo $newVar
67890
=')
kent$ echo $newVar
67890
=')
example:
例子:
##代码##if your text was stored in var testString
, you could:
如果您的文本存储在 var 中testString
,您可以: