如何使用输入一些shell变量的Bash来创建一个TSV文件?(How to crete a TSV file using Bash that takes as input some shell va

编程入门 行业动态 更新时间:2024-10-22 10:58:33
如何使用输入一些shell变量的Bash来创建一个TSV文件?(How to crete a TSV file using Bash that takes as input some shell variables?)

我有一些shell变量等于不同类型的值:

variable 1: 72.9% variable 2: 27.1% variable 3: Y variable 4: 8756

我希望能够将这些变量的值打印到制表符分隔的文件中,甚至可以将变量的名称作为列标题

输出:

variable1 variable2 variable3 variable4 72.9% 27.1% Y 8756

有任何想法吗?

I have some shell variables which equal different types of values :

variable 1: 72.9% variable 2: 27.1% variable 3: Y variable 4: 8756

I want to be able to print the values of these variables to a tab separated file and possibly even have the name of the variables as column headers

output:

variable1 variable2 variable3 variable4 72.9% 27.1% Y 8756

Any ideas?

最满意答案

相对容易,你只需要将每行一次读入单个数组变量,然后提供格式化的输出,例如

#!/bin/bash declare -a name declare -a num declare -a value while read -r a b c; do name+=( "$a" ) num+=( "$b" ) value+=( "$c" ) done < "$1" ## C-style loop used to index both name & num for headings for ((i = 0; i < ${#name[@]}; i++)); do printf "%s\t" "${name[i]}${num[i]%:}" done echo for i in "${value[@]}"; do printf "%s\t\t" "$i" done echo

这将导致制表符分隔的标题和值(您可能需要稍微播放一下空格 - 例如,在值输出上使用2个制表符)

示例使用/输出

$ bash headings.sh csvdata.txt variable1 variable2 variable3 variable4 72.9% 27.1% Y 8756

如果脚本本身有变量,则必须采用相同的方法。 使用变量时,您有名称,但需要创建一个包含名称的数组以及值,以循环访问值以提供所需的输出。 无论你是写一个temp_file并读取其中的值,还是使用数组来存储变量的名称(通过字符串连接在上面的数字num之间创建),过程将是相同的。


变量已经在脚本中

如上所述,您将采取类似的方法,只有在这里,您可以选择标题前缀,然后使用循环计数器将数字添加到您选择的任何名称的末尾,然后简单地循环存储在数组,例如

#!/bin/bash foo="72.9%" bar="27.1%" baz="Y" buz="8756" declare -a value value=( "$foo" "$bar" "$baz" "$buz" ) for ((i = 0; i < ${#value[@]}; i++)); do printf "%s\t" "variable$((i+1))" done echo for i in "${value[@]}"; do printf "%s\t\t" "$i" done echo

示例使用/输出

(一样)

$ bash headings2.sh variable1 variable2 variable3 variable4 72.9% 27.1% Y 8756

Relatively easy, you just need to read the values one line-at-a-time into individual array variables and then provide the formatted output, e.g.

#!/bin/bash declare -a name declare -a num declare -a value while read -r a b c; do name+=( "$a" ) num+=( "$b" ) value+=( "$c" ) done < "$1" ## C-style loop used to index both name & num for headings for ((i = 0; i < ${#name[@]}; i++)); do printf "%s\t" "${name[i]}${num[i]%:}" done echo for i in "${value[@]}"; do printf "%s\t\t" "$i" done echo

Which will result in tab separated headings and values (you may need to play with the spacing a bit -- e.g. using 2 tabs on value output)

Example Use/Output

$ bash headings.sh csvdata.txt variable1 variable2 variable3 variable4 72.9% 27.1% Y 8756

If you have the variables in the script itself, you will have to take the same approach. With a variable, you have the name, but will need to create an array holding the names, as well as the values in order to loop over the values to provide the output you want. Whether you write a temp_file and read the values in, or use arrays to store the names of the variables (created by string concatenation between the number num above) the process will be the same.


Variables Already In Script

As mentioned above, you will take a similar approach, only here, you choose the heading prefix, and just use the loop counter to add the number at the end of whatever name you choose, then simply loop over the values you have stored in the array, e.g.

#!/bin/bash foo="72.9%" bar="27.1%" baz="Y" buz="8756" declare -a value value=( "$foo" "$bar" "$baz" "$buz" ) for ((i = 0; i < ${#value[@]}; i++)); do printf "%s\t" "variable$((i+1))" done echo for i in "${value[@]}"; do printf "%s\t\t" "$i" done echo

Example Use/Output

(the same)

$ bash headings2.sh variable1 variable2 variable3 variable4 72.9% 27.1% Y 8756

更多推荐

本文发布于:2023-08-06 04:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1444453.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   创建一个   变量   文件   Bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!