bash readarray 的替代方案,因为它在 mac os x 上不起作用

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

alternative to readarray, because it does not work on mac os x

macosperlbashawk

提问by user3652962

I have a varsValues.txt file

我有一个 varsValues.txt 文件

cat varsValues.txt
aa=13.7
something=20.6
countries=205
world=1
languages=2014
people=7.2
oceans=3.4

And I would like to create 2 arrays, vars and values. It should contain

我想创建 2 个数组、变量和值。它应该包含

echo ${vars[@]}
aa something countries world languages people oceans

echo ${values[@]}
13.7 20.6 205 1 2014 7.2 3.4

I use

我用

Npars=7

readarray -t vars < <(cut -d '=' -f1 varsValues.txt)
readarray -t values < <(cut -d '=' -f2 varsValues.txt)

for (( yy=0; yy<$Npars; yy++ )); do
eval ${vars[$yy]}=${values[$yy]}
done

echo $people
7.2

But I would like it without readarray which does not work on Mac (os x) and IFS (interfield separater).

但我希望它没有 readarray ,它不适用于 Mac(os x)和 IFS(场间分离器)。

Any other solution? awk? perl? which I can use in my bash script.

还有其他解决方案吗?啊?珀尔?我可以在我的 bash 脚本中使用它。

Thanks.

谢谢。

采纳答案by S. Ahn

Here's the awk version. Note that NParsis not hardcoded.

这是 awk 版本。请注意,这NPars不是硬编码的。

vars=($(awk -F= '{print }' varsValues.txt))
values=($(awk -F= '{print }' varsValues.txt))

Npars=${#vars[@]}

for ((i=0; i<$Npars; i++)); do
    eval ${vars[$i]}=${values[$i]}
done

echo $people

回答by John B

You could use a read loop.

您可以使用读取循环。

while IFS=\= read var value; do
    vars+=($var)
    values+=($value)
done < VarsValues.txt

回答by Ed Morton

Let's start with this:

让我们从这个开始:

$ awk -F'=' '{values[]=} END{print values["people"]}' file
7.2

$ awk -F'=' '{values[]=} END{for (name in values) print name, values[name]}' file
languages 2014
oceans 3.4
world 1
something 20.6
countries 205
people 7.2
aa 13.7

Now - what else do you need to do?

现在 - 你还需要做什么?

回答by Сухой27

perl -0777 -nE '@F= split /[=\r\n]/; say "@F[grep !($_%2), 0..$#F]"; say "@F[grep $_%2, 0..$#F]"' varsValues.txt

or by reading same file twice,

或者通过两次读取同一个文件,

perl -F'=' -lane 'print $F[0]' varsValues.txt
perl -F'=' -lane 'print $F[1]' varsValues.txt

回答by anubhava

You can use declarebulletin:

您可以使用declare公告:

declare -a vars=( $(cut -d '=' -f1 varsValues.txt) )
declare -a values=( $(cut -d '=' -f2 varsValues.txt) )

回答by Adrian

Figured I'd toss this in here: https://raw.githubusercontent.com/AdrianTP/new-environment-setup/master/utils/readarray.sh

想我会把它扔在这里:https: //raw.githubusercontent.com/AdrianTP/new-environment-setup/master/utils/readarray.sh

#!/bin/bash
# from: https://peniwize.wordpress.com/2011/04/09/how-to-read-all-lines-of-a-file-into-a-bash-array/
readarray() {
  local __resultvar=
  declare -a __local_array
  let i=0
  while IFS=$'\n' read -r line_data; do
      __local_array[i]=${line_data}
      ((++i))
  done < 
  if [[ "$__resultvar" ]]; then
    eval $__resultvar="'${__local_array[@]}'"
  else
    echo "${__local_array[@]}"
  fi
}

I keep this in a "utils" folder in my "new-environment-setup" Github repo, and I just clone it down and import it whenever I need to read a file into an array of lines an array get a new computer or wipe my drive. It should thus act as a backfill for readarray's shortcomings on Mac.

我将它保存在我的“新环境设置”Github 存储库中的“utils”文件夹中,我只是将其克隆并在需要将文件读入行数组时导入它,数组获取新计算机或擦除我的驱动器。因此,它应该作为 readarray 在 Mac 上的缺点的回填。

Import looks like:

导入看起来像:

# From: https://stackoverflow.com/a/12694189/771948
DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
. "$DIR/utils/readarray.sh"

Usage looks like readarray "<output_var_name>" "<input_file_name>".

用法看起来像readarray "<output_var_name>" "<input_file_name>"

Yes it's a little rough. Sorry about that. It may not even work correctly anymore, but it did at one point, so I thought I would share it here to plant the idea of simply...writing your own backfill.

是的,它有点粗糙。对于那个很抱歉。它甚至可能不再正常工作,但有一次它确实如此,所以我想我会在这里分享它以植入简单的想法......编写自己的回填。

回答by Noel Yap

Try:

尝试:

IFS=$'\n' vars=($(cut -d '=' -f1 varsValues.txt))
IFS=$'\n' values=($(cut -d '=' -f2 varsValues.txt))