bash 如何解析/捕获由破折号分隔的字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35979238/
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
How can I parse/capture strings separated by dashes?
提问by Gregg
In a Bash shell script, I'm processing data that starts off like this:
在 Bash shell 脚本中,我正在处理这样开始的数据:
string1-string2-string3-string4-etc
I need string1
and string2
assigned to unique variables, and string3-string4-etc
left together inside of another single unique variable. I played around with trying to set IFS but then string3
, string4
, and etc
were disconnected.
我需要string1
并string2
分配给唯一变量,并string3-string4-etc
一起留在另一个唯一变量中。我试图设置IFS但随后发挥各地string3
,string4
以及etc
为断开。
How can I get the data I want? I'd prefer builtin shell commands if possible, but gawk or other tools are fine too.
我怎样才能得到我想要的数据?如果可能的话,我更喜欢内置的 shell 命令,但 gawk 或其他工具也很好。
回答by chepner
Use the built-in read
command:
使用内置read
命令:
str='string1-string2-string3-string4-etc'
IFS=- read str1 str2 the_rest <<< "$str"
回答by Todd A. Jacobs
Use Cut and Command Substitution to Capture Fields
使用剪切和命令替换来捕获字段
As long as the -
character is always a field separator and not embedded in any substrings, the following will work:
只要-
字符始终是字段分隔符并且未嵌入任何子字符串中,以下内容将起作用:
str='string1-string2-string3-string4-etc'
a=$(echo "$str" | cut -d- -f1)
b=$(echo "$str" | cut -d- -f2)
c=$(echo "$str" | cut -d- -f3-)
The cut utility does the work of using the dash as a delimiter to define the fields you want to capture, and Bash command substitutionis used to assign the output from cut to a variable.
cut 实用程序使用破折号作为分隔符来定义要捕获的字段,而 Bash命令替换用于将 cut 的输出分配给变量。
Validation
验证
$ echo "$a"; echo "$b"; echo "$c"
string1
string2
string3-string4-etc
回答by Jahid
Using Bash regex:
使用 Bash 正则表达式:
s=string1-string2-string3-string4-etc
pat="([^-]*)-([^-]*)-(.*)"
[[ $s =~ $pat ]]
echo "${BASH_REMATCH[1]}"
echo "${BASH_REMATCH[2]}"
echo "${BASH_REMATCH[3]}"
Output:
输出:
string1
string2
string3-string4-etc