[英]How to use paste command recursively in a shell script
I have a paste command in my script. 我的脚本中有粘贴命令。 But it is not giving my desired output.
但这没有给出我想要的输出。 my datafile is
我的数据文件是
file.txt
1
3
5
4
my shell script is 我的shell脚本是
f1=file.txt
for j in 1..12
do
awk '{printf ("%.1f\n", $1+'$j');}' $f1 > $j_$f1
paste $j_$f1 > ofile.txt # ofile.txt is a new empty file
done
This script gives output as 该脚本将输出显示为
ofile.txt
2
4
6
5
3
5
7
6
.
.
My desired output as 我想要的输出为
ofile.txt
2 3 4 5 6 ... 13
4 5 6 7 8 ... 15
6 7 8 9 10 .. 17
5 6 7 8 9 ... 16
I also want to do for the following script 我也想为以下脚本做
bonus_company1=10
bonus_company2=25
bonus_company3=50
declare var=("company1" "company2" "company3")
for k in var{[@]}
do for j in jan..dec
do if [ $j == "jan" ] || [ $j == "may" ] || [ $j == "sep" ]; then
awk '{printf ("%.1f\n", $1+ '$bonus_$k');}' $f1 > $k_$j_$f1
elif [ $j -ne "jan" ] && [ $j -ne "may" ] && [ $j -ne "sep" ]; then
awk '{printf ("%.1f\n", $1+5);}' $f1 > $k_$j_$f1
fi
paste $k_$j_$f1 > $k.txt # $k.txt is a new empty file
done
done
so I will get 3 different files for three companies. 因此我将为三个公司获得3个不同的文件。 eg
例如
company1.txt
11 6 6 6 11 .....
13 8 8 8 11 .....
15 10 10 10 15 .....
14 9 9 9 14 .....
I would suggest doing the whole thing in awk, rather than using a shell loop: 我建议在awk中完成整个操作,而不是使用shell循环:
awk '{for (i=1; i<=12; ++i) printf "%d%s", $1+i, (i<12?FS:RS)}' "$f1" > output
This loop runs for each line of the file and outputs all of the columns, saving the need to use another tool such as paste
. 该循环针对文件的每一行运行,并输出所有列,从而节省了使用其他工具(如
paste
。 The ternary operator is used to output a space ( FS
) between each column and a newline ( RS
) at the end of the line. 三元运算符用于在每列和行尾的换行符(
RS
)之间输出一个空格( FS
)。
For the second part of your question, you can still do most of the work in awk. 对于问题的第二部分,您仍然可以用awk完成大部分工作。
declare -A companies
companies=( [company1]=10 [company2]=25 [company3]=50 )
for c in "${companies[@]}"; do
awk -v bonus="${companies[c]}" '{for (i=1; i<=12; ++i)
printf "%d%s", $i+(i==1||i==5||i==9?bonus:5), (i<12?FS:RS)}' "$f1" > "$c.txt"
done
I have created an associative array containing each company name and their bonus. 我创建了一个包含每个公司名称及其奖金的关联数组。 The loop goes through each company in the array, passing the bonus to awk as a variable (note that I'm doing this using
-v
rather than using string concatenation). 循环遍历数组中的每个公司,将奖金作为变量传递给awk(请注意,我正在使用
-v
而不是使用字符串串联)。 The loop goes from 1 to 12, adding the bonus for months 1, 5 and 9, or 5 otherwise. 循环从1到12,增加第1、5和9个月的奖金,否则增加5个月的奖金。 The name of the company (the key of the shell array) is used for the output file.
公司名称(shell数组的键)用于输出文件。
I can't understand, why are you going in such complicated way. 我不明白,你为什么要这么复杂。 You can do it in a more simpler way:
您可以通过更简单的方式进行操作:
for 2nd one: 对于第二个:
for k in var{[@]}
do awk '{printf("%.1f %.1f %.1f %.1f %.1f %.1f %.1f %.1f %.1f %.1f %.1f %.1f\n", $1+"$bonus_$k", $1+5, $1+5, $1+5, $1+"$bonus_$k", $1+5, $1+5, $1+5, $1+"$bonus_$k", $1+5, $1+5, $1+5);}' file.txt > $k_file.txt
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.