简体   繁体   English

在bash数组中获取shell命令的输出

[英]Getting output of shell command in bash array

I have a uniq -c output, that outputs about 7-10 lines with the count of each pattern that was repeated for each unique line pattern. 我有一个uniq -c输出,它输出大约7-10行,并为每个唯一的行模式重复了每个模式的计数。 I want to store the output of my uniq -c file.txt into a bash array. 我想将uniq -c file.txt的输出存储到bash数组中。 Right now all I can do is store the output into a variable and print it. 现在,我所能做的就是将输出存储到变量中并打印出来。 However, bash currently thinks the entire output is just one big string. 但是,bash当前认为整个输出只是一个很大的字符串。

How does bash recognize delimiters? bash如何识别分隔符? How do you store UNIX shell command output as Bash arrays? 如何将UNIX shell命令输出存储为Bash数组?

Here is my current code: 这是我当前的代码:

proVar=`awk '{printf ("%s\t\n"), $1}' file.txt | grep -P 'pattern' | uniq -c`
echo $proVar

And current output I get: 而我得到的电流输出为:

587 chr1 578 chr2 359 chr3 412 chr4 495 chr5 362 chr6 287 chr7 408 chr8 285 chr9 287 chr10 305 chr11 446 chr12 247 chr13 307 chr14 308 chr15 365 chr16 342 chr17 245 chr18 252 chr19 210 chr20 193 chr21 173 chr22 145 chrX 58 chrY

Here is what I want: 这是我想要的:

proVar[1] = 2051
proVar[2] = 1243
proVar[3] = 1068
...
proVar[22] = 814 
proVar[X] = 72
proVar[Y] = 13 

In the long run, I'm hoping to make a barplot based on the counts for each index, where every 50 counts equals one "=" sign. 从长远来看,我希望基于每个索引的计数创建一个小图,其中每50个计数等于一个“ =”号。 It will hopefully look like the below 希望看起来像下面

chr1 ===========
chr2 ===========
chr3 =======
chr4 =========
...
chrX ==
chrY =

Any help, guys? 大家有帮助吗?

To build the associative array, try this: 要构建关联数组,请尝试以下操作:

declare -A proVar
while read -r val key; do
  proVar[${key#chr}]=$val
done < <(awk '{printf ("%s\t\n"), $1}' file.txt | grep -P 'pattern' | uniq -c)

Note: This assumes that your command's output is composed of multiple lines, each containing one key-value pair; 注意:这假定命令的输出由行组成,每行包含对键值对; the single-line output shown in your question comes from passing $proVar to echo without double quotes. 问题中显示的单行输出来自传递$proVar到echo而不带双引号的情况。

  • Uses a while loop to read each output line from a process substitution ( <(...) ). 使用while循环从进程替换( <(...) )中读取每条输出行。
  • The key for each assoc. 每个assoc的密钥 array entry is formed by stripping prefix chr from each input line's first whitespace-separated token, whereas the value is the rest of the line (after the separating space). 数组条目是通过从每个输入行的第一个空格分隔的标记中chr前缀chr来形成的,而该是行的其余部分(在分隔空间之后)。

To then create the bar plot, use: 要创建条形图,请使用:

while IFS= read -r key; do
  echo "chr${key} $(printf '=%.s' $(seq $(( ${proVar[$key]} / 50 ))))"
done < <(printf '%s\n' "${!proVar[@]}" | sort -n)

Note: Using sort -n to sort the keys will put non-numeric keys such as X and Y before numeric ones in the output. 注意:使用sort -n对键进行排序会将XY类的非数字键放在输出的数字键之前

  • $(( ${proVar[$key]} / 50 )) calculates the number of = chars. $(( ${proVar[$key]} / 50 ))计算=字符数。 to display, using integer division in an arithmetic expansion. 显示,在算术扩展中使用整数除法。
  • The purpose of $(seq ...) is to simply create as many tokens (arguments) as = chars. $(seq ...)的目的是简单地创建与= chars一样多的标记 (参数)。 should be displayed (the tokens created are numbers, but their content doesn't matter). 应该显示(创建的令牌是数字,但其内容无关紧要)。
  • printf '=%.s' ... is a trick that effectively prints as many = chars. printf '=%.s' ...是一种技巧,可以有效地打印出许多=字符。 as there are arguments following the format string. 因为格式字符串后面有参数。
  • printf '%s\\n' "${!proVar[@]}" | sort -n printf '%s\\n' "${!proVar[@]}" | sort -n sorts the keys of the assoc. printf '%s\\n' "${!proVar[@]}" | sort -n对assoc的键进行printf '%s\\n' "${!proVar[@]}" | sort -n array numerically, and its output is fed via a process substitution to the while loop, which therefore iterates over the keys in sorted order. 数组以数字方式表示,其输出通过进程替代输入到while循环,因此该循环以排序顺序遍历键。

You can create an array in an assignment using parentheses: 您可以使用括号在分配中创建数组:

proVar=(`awk '{printf ("%s\t\n"), $1}' file.txt | grep -P 'pattern' | uniq -c`)

There's no built-in way to create an associative array directly from input. 没有内置的方法可以直接从输入创建关联数组。 For that you'll need an additional loop. 为此,您需要一个附加循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM