将输出重定向到 bash 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1753366/
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
Redirect output to a bash array
提问by calvinkrishy
I have a file containing the string
我有一个包含字符串的文件
ipAddress=10.78.90.137;10.78.90.149
I'd like to place these two IP addresses in a bash array. To achieve that I tried the following:
我想将这两个 IP 地址放在一个 bash 数组中。为了实现这一点,我尝试了以下方法:
n=$(grep -i ipaddress /opt/ipfile | cut -d'=' -f2 | tr ';' ' ')
This results in extracting the values alright but for some reason the size of the array is returned as 1 and I notice that both the values are identified as the first element in the array. That is
这导致提取值没问题,但由于某种原因,数组的大小返回为 1,我注意到这两个值都被标识为数组中的第一个元素。那是
echo ${n[0]}
returns
返回
10.78.90.137 10.78.90.149
How do I fix this?
我该如何解决?
Thanks for the help!
谢谢您的帮助!
回答by ghostdog74
do you really need an array
你真的需要一个数组吗
bash
猛击
$ ipAddress="10.78.90.137;10.78.90.149"
$ IFS=";"
$ set -- $ipAddress
$ echo
10.78.90.137
$ echo
10.78.90.149
$ unset IFS
$ echo $@ #this is "array"
if you want to put into array
如果你想放入数组
$ a=( $@ )
$ echo ${a[0]}
10.78.90.137
$ echo ${a[1]}
10.78.90.149
@OP, regarding your method: set your IFS to a space
@OP,关于你的方法:将你的 IFS 设置为一个空格
$ IFS=" "
$ n=( $(grep -i ipaddress file | cut -d'=' -f2 | tr ';' ' ' | sed 's/"//g' ) )
$ echo ${n[1]}
10.78.90.149
$ echo ${n[0]}
10.78.90.137
$ unset IFS
Also, there is no need to use so many tools. you can just use awk, or simply the bash shell
此外,没有必要使用这么多工具。你可以只使用 awk,或者简单地使用 bash shell
#!/bin/bash
declare -a arr
while IFS="=" read -r caption addresses
do
case "$caption" in
ipAddress*)
addresses=${addresses//[\"]/}
arr=( ${arr[@]} ${addresses//;/ } )
esac
done < "file"
echo ${arr[@]}
output
输出
$ more file
foo
bar
ipAddress="10.78.91.138;10.78.90.150;10.77.1.101"
foo1
ipAddress="10.78.90.137;10.78.90.149"
bar1
$./shell.sh
10.78.91.138 10.78.90.150 10.77.1.101 10.78.90.137 10.78.90.149
gawk
呆呆的
$ n=( $(gawk -F"=" '/ipAddress/{gsub(/\"/,"",);gsub(/;/," ",) ;printf " "}' file) )
$ echo ${n[@]}
10.78.91.138 10.78.90.150 10.77.1.101 10.78.90.137 10.78.90.149
回答by Joy Dutta
This one works:
这个有效:
n=(`grep -i ipaddress filename | cut -d"=" -f2 | tr ';' ' '`)
EDIT: (improved, nestable version as per Dennis)
编辑:(根据丹尼斯的改进,可嵌套版本)
n=($(grep -i ipaddress filename | cut -d"=" -f2 | tr ';' ' '))
回答by Paused until further notice.
A variation on a theme:
一个主题的变体:
$ line=$(grep -i ipaddress /opt/ipfile)
$ saveIFS="$IFS" # always save it and put it back to be safe
$ IFS="=;"
$ n=($line)
$ IFS="$saveIFS"
$ echo ${n[0]}
ipAddress
$ echo ${n[1]}
10.78.90.137
$ echo ${n[2]}
10.78.90.149
If the file has no other contents, you may not need the grep
and you could read in the whole file.
如果文件没有其他内容,您可能不需要grep
并且您可以读取整个文件。
$ saveIFS="$IFS"
$ IFS="=;"
$ n=$(</opt/ipfile)
$ IFS="$saveIFS"
回答by J. A. Faucett
A Perl solution:
Perl 解决方案:
n=($(perl -ne 's/ipAddress=(.*);/ / && print' filename))
which tests for and removes the unwanted characters in one operation.
它在一次操作中测试并删除不需要的字符。
回答by rashok
You can do this by using IFS
in bash
.
您可以通过使用IFS
in来做到这一点bash
。
- First read the first line from file.
- Seoncd convert that to an array with
=
as delimeter. - Third convert the value to an array with
;
as delimeter.
- 首先从文件中读取第一行。
- Seoncd 将其转换为带有
=
分隔符的数组。 - 第三次将值转换为带有
;
分隔符的数组。
Thats it !!!
就是这样 !!!
#!/bin/bash
IFS='\n' read -r lstr < "a.txt"
IFS='=' read -r -a lstr_arr <<< $lstr
IFS=';' read -r -a ip_arr <<< ${lstr_arr[1]}
echo ${ip_arr[0]}
echo ${ip_arr[1]}