如何将变量打印到两列或多列的输出文件中(echo
例如使用)。我有以下内容:
grep -oP 'value\s*=\s*\K.*' file >x_vs_y # x values
for X in $(seq 0 50 400)
do
echo "$X" >>x_vs_y # y values
done
输出:(x_vs_y)
1.08759432363
1.08477216702
1.08211988431
1.07947977045
1.07685185457
1.07423631941
1.07163282502
1.06904156798
1.06646245052
0
50
100
150
200
250
300
350
400
使用此脚本,我在单列 (18x1) 中获得输出文件,但我想获得 9x2 数组(X 与 Y),例如:
输出:(x_vs_y)
1.08759432363 0
1.08477216702 50
1.08211988431 100
1.07947977045 150
1.07685185457 200
1.07423631941 250
1.07163282502 300
1.06904156798 350
1.06646245052 400
答案1
考虑这个输入文件:
$ cat file
value = 1.08759432363
value = 1.08477216702
value = 1.08211988431
value = 1.07947977045
value = 1.07685185457
value = 1.07423631941
value = 1.07163282502
value = 1.06904156798
value = 1.06646245052
要打印两列:
$ awk '{print x+0, $3; x+=50}' file
0 1.08759432363
50 1.08477216702
100 1.08211988431
150 1.07947977045
200 1.07685185457
250 1.07423631941
300 1.07163282502
350 1.06904156798
400 1.06646245052
将输出保存在文件中
$ awk '0==a[$0]++' file >outfile
$ cat outfile
value = 1.08759432363
value = 1.08477216702
value = 1.08211988431
value = 1.07947977045
value = 1.07685185457
value = 1.07423631941
value = 1.07163282502
value = 1.06904156798
value = 1.06646245052
答案2
paste
也许和的组合seq
可以更简单:
paste datafile <( seq 0 50 $(( ($(wc -l datafile | cut -d' ' -f1 )-1)*50 )) ) > output
的最后一个值seq
仅基于输入数据文件中的行号。