我有两个文件,即timedata.txt和veldata.txt。以下是timedata文件中的示例数据。
Time = 0
Time = 0.000112
Time = 0.000224
Time = 0.000336
Time = 0.000448
Time = 0.00056
Time = 0.000672
Time = 0.000784
Time = 0.000896
Time = 0.001008
Time = 0.00112
以下是veldata.txt中的示例数据
Average of volScalarField over patch OUTFLOW[0] = 0
Average of volScalarField over patch OUTFLOW[0] = 7.54999
Average of volScalarField over patch OUTFLOW[0] = 7.67539
Average of volScalarField over patch OUTFLOW[0] = 7.81389
Average of volScalarField over patch OUTFLOW[0] = 7.83651
Average of volScalarField over patch OUTFLOW[0] = 7.85014
Average of volScalarField over patch OUTFLOW[0] = 7.87054
Average of volScalarField over patch OUTFLOW[0] = 7.87814
Average of volScalarField over patch OUTFLOW[0] = 7.87423
Average of volScalarField over patch OUTFLOW[0] = 7.87499
Average of volScalarField over patch OUTFLOW[0] = 7.8768
我需要来自这两个文件的数值数据并将其合并到一个dat文件中,其中一列作为时间数据,另一列作为速度数据。所以我可以使用gnuplot绘制这些值。
所需的解决方案如下所示:
0 0
0.000112 7.54999
0.000224 7.67539
0.000336 7.81389
0.000448 7.83651
依旧......
我想使用bash编写脚本,以便我可以自动执行该过程。我是使用linux和bash脚本的新手,因此我不知道很多选项。
欢迎任何帮助。谢谢
答案 0 :(得分:4)
awk
救援!
paste <(awk '{print $NF}' file1) <(awk '{print $NF}' file2)
如果需要多于两个文件或许这个
$ p() { awk '{print $NF}' $1;}; paste <(p file1) <(p file2)
这个生成的制表符分隔了列,你可以使用column
的管道更好,如
$ p() { awk '{print $NF}' $1;}; paste <(p file1) <(p file2) | column -t
将导致
0 0
0.000112 7.54999
0.000224 7.67539
0.000336 7.81389
0.000448 7.83651
0.00056 7.85014
0.000672 7.87054
0.000784 7.87814
0.000896 7.87423
0.001008 7.87499
0.00112 7.8768
答案 1 :(得分:3)
获得此输出的单个awk
命令:
awk -v OFS='\t' 'FNR==NR{a[FNR]=$NF; next} {print a[FNR], $NF}' timedata.txt veldata.txt
要获取表格格式的输出,请使用printf
:
awk 'FNR==NR{a[FNR]=$NF; next} {printf "%-12s\t%s\n", a[FNR], $NF}' time.txt veldata.txt
0 0
0.000112 7.54999
0.000224 7.67539
0.000336 7.81389
0.000448 7.83651
0.00056 7.85014
0.000672 7.87054
0.000784 7.87814
0.000896 7.87423
0.001008 7.87499
0.00112 7.8768