使用Bash将两个字符之间的所有字符串提取到数组中 [英] Extract all Strings between two characters to array using Bash

查看:78
本文介绍了使用Bash将两个字符之间的所有字符串提取到数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了我们的,但是找不到解决方案,无法使用Bash将两个字符之间的所有String提取到数组中.

i searched ours but can't find a solution to extract all Strings between two characters to array using Bash.

我找到

sed -n 's/.*\[\(.*\)\].*/\1/p'

但这只会显示最后一个条目.

But this only show me the last entry.

我的字符串看起来像:

var="[a1] [b1] [123] [Text text] [0x0]"

我想要一个这样的数组:

I want a Array like this:

arr[0]="a1"
arr[1]="b1"
arr[2]="123"
arr[3]="Text text"
arr[4]="0x0"

因此,我在[和]之间搜索Stings并将其加载到没有[和]的数组中.

So i search for Stings between [ and ] and load it into an Array without [ and ].

感谢您的帮助!

推荐答案

在这里已经有很多建议可能对您有用,但可能并不取决于您的数据.例如,除非您的字段中嵌入了逗号,否则用] [的当前字段分隔符代替逗号是可行的.您的样本数据中没有哪一个,但从未有人知道. :)

There are a lot of suggestions that may work for you here already, but may not depending on your data. For example, substituting your current field separator of ] [ for a comma works unless you have commas embedded in your fields. Which your sample data does not have, but one never knows. :)

一个理想的解决方案是使用某种东西作为字段分隔符,以确保它永远不会成为您字段的一部分,例如null.但这很难以可移植的方式进行(即在不知道可用工具的情况下).因此,不太极端的立场可能是使用换行符作为分隔符:

An ideal solution would be to use something as a field separator that is guaranteed never to be part of your field, like a null. But that's hard to do in a portable way (i.e. without knowing what tools are available). So a less extreme stance might be to use a newline as a separator:

var="[a1] [b1] [123] [Text text] [0x0]"

mapfile -t arr < <(sed $'s/^\[//;s/] \[/\\\n/g;s/]$//' <<<"$var")

declare -p arr

这将导致:

declare -a arr='([0]="a1" [1]="b1" [2]="123" [3]="Text text" [4]="0x0")'

这在功能上等同于Inian提供的awk解决方案.请注意,mapfile需要bash版本4或更高版本.

This is functionally equivalent to the awk solution that Inian provided. Note that mapfile requires bash version 4 or above.

也就是说,您也可以只在bash中执行此操作,而无需依赖sed之类的任何外部工具:

That said, you could also this exclusively within bash, without relying on any external tools like sed:

arr=( $var )

last=0
for i in "${!arr[@]}"; do
  if [[ ${arr[$i]} != \[* ]]; then
    arr[$last]="${arr[$last]} ${arr[$i]}"
    unset arr[$i] 
    continue
  fi
  last=$i
done

for i in "${!arr[@]}"; do
  arr[$i]="${arr[$i]:1:$((${#arr[$i]}-2))}"
done

此时,declare -p arr结果为:

declare -a arr='([0]="a1" [1]="b1" [2]="123" [3]="Text text" [5]="0x0")'

这会将您的$var吸入字段由空格分隔的数组$arr[]中,然后根据它们是否以方括号开头折叠字段.然后,它遍历各个字段,并用消除了第一个和最后一个字符的子字符串替换它们.它的弹性可能稍差一些,而且较难阅读,但全部都在bash中. :)

This sucks your $var into the array $arr[] with fields separated by whitespace, then it collapses the fields based on whether they begin with a square bracket. It then goes through the fields and replaces them with the substring that eliminates the first and last character. It may be a little less resilient and harder to read, but it's all within bash. :)

这篇关于使用Bash将两个字符之间的所有字符串提取到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆