bash 使用可变分隔符拆分字符串的 shell 脚本命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19885660/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:34:40  来源:igfitidea点击:

shell script command to split string using a variable delimiter

bashshell

提问by Soumya

I want to split a string in a bash shell script with the following conditions.

我想在具有以下条件的 bash shell 脚本中拆分字符串。

1) The delimiter is a variable

1) 分隔符是一个变量

2) The delimiter is multicharacater

2) 分隔符是多字符

example:

例子:

A quick brown fox
var=brown

I want to split the string into A quickand brown foxbut using the variable varas delimiter and not brown

我想将字符串拆分为A quickandbrown fox但使用变量var作为分隔符而不是brown

回答by jekell

#!/bin/bash

#variables according to the example
example="A quick brown fox"
var="brown"

#the logic (using bash string replacement)
front=${example%${var}*}
rear=${example#*${var}}

#the output
echo "${front}"
echo "${var}"
echo "${rear}"

回答by cforbish

This sounds like what you are actually asking for (keep the delimiter in results):

这听起来像您实际要求的(在结果中保留分隔符):

str="A quick brown fox"
var="brown "
result=$(echo ${str} | sed "s/${var}/\n${var}/g")

This is what you might have actually meant (remove the delimiter from the original string):

这就是您的实际意思(从原始字符串中删除分隔符):

str="A quick really brown fox"
var=" really "
result=$(echo ${str} | sed "s/${var}/\n/g")

This is something you can run to verify the results:

这是您可以运行以验证结果的内容:

IFS=$'\n'
for item in ${result[@]}; do
    echo "item=${item}."
done

回答by petrus4

result=$(echo "${str}" | awk 'gsub("${var}","${var}\n")

回答by gniourf_gniourf

Your question isn't very well posed, because your splitting should show "A quick "(with a trailing space). You don't even tell how you want the output to be given (in an array, in two separate variables...). Never, let me rephrase your question in my way, and answer this rephrased question. If it's not what you want, you'll know how to modify your original post.

您的问题不是很好,因为您的拆分应该显示"A quick "(带有尾随空格)。你甚至不知道你希望如何给出输出(在一个数组中,在两个单独的变量中......)。永远不要,让我用我的方式重新表述你的问题,然后回答这个重新表述的问题。如果这不是您想要的,您将知道如何修改您的原始帖子。

Given a string sand a regex var, give an array awith two fields a[0]and a[1]such that the concatenation of the two fields of array ais sand such that a[1] =~ ^var.*with a[1]being the minimal (non-greedy) match.

如果给定字符串s和一个正则表达式var,得到的阵列a具有两个字段a[0]a[1],使得阵列的两个字段的级联as,并且使得a[1] =~ ^var.*a[1]作为最小(非贪婪)匹配。

Well, this already exists in bash:

嗯,这已经存在于 bash 中:

[[ $s =~ ^(.*)($var.*)$ ]]
a=( "${BASH_REMATCH[@]:1}" )

Look:

看:

$ s="A quick brown fox"
$ var=brown
$ [[ $s =~ ^(.*)($var.*)$ ]]
$ a=( "${BASH_REMATCH[@]:1}" )
$ declare -p a
declare -a a='([0]="A quick " [1]="brown fox")'
$

回答by thom

It can be done with 100% bash internal commands:

可以使用 100% bash 内部命令来完成:

#!/bin/bash

#variables according to the example
example="A quick brown fox"
var="brown"

#the logic (using bash string replacement)
front=${example%"$var" *}
rear=${example/"$front"/}

#the output
echo "$front"
echo "$rear"