我有一些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: 8756I 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 8756Any 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 8756Relatively 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 echoWhich 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 8756If 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 echoExample Use/Output
(the same)
$ bash headings2.sh variable1 variable2 variable3 variable4 72.9% 27.1% Y 8756更多推荐
发布评论