[英]bash script to read table line by line
Sample Input: (tab separated values in table format) 输入样本:(以表格格式制表符分隔的值)
Vserver Volume Aggregate State Type Size Available Used%
--------- ------------ ------------ ---------- ---- ---------- ---------- -----
vs1 vol1 aggr1 online RW 2GB 1.9GB 5%
vs1 vol1_dr aggr0_dp online DP 200GB 160.0GB 20%
vs1 vol2 aggr0 online RW 150GB 110.3GB 26%
vs1 vol2_dr aggr0_dp online DP 150GB 110.3GB 26%
vs1 vol3 aggr1 online RW 150GB 120.0GB 20%
I've a task to find the volumes under an aggregate which has breached threshold so that they can be moved to a different aggregate. 我的任务是在已超出阈值的聚合下查找卷,以便可以将其移动到其他聚合。 Need your help to read the above table line by line, capture volume associated with a specific aggregate name (which will passed as an argument) and add the size of the volume to variable (say total). 需要您的帮助来逐行阅读上表,捕获与特定聚合名称相关联的卷(将作为参数传递),并将卷的大小添加到变量中(例如合计)。 The next lines should be read till the variable, total is less than or equal to the size that should be moved (again which will passed as an argument) 应读取下一行,直到变量total小于或等于应移动的大小为止(再次将其作为参数传递)
Expected output if <aggr1>
and <152GB>
are passed as arguments 如果将<aggr1>
和<152GB>
作为参数传递,则预期输出
vol1 aggr1 2GB
vol3 aggr1 150GB
You want to read the file line by line, so you can use awk. 您要逐行读取文件,因此可以使用awk。 You give arguments with the syntax -v aggr=<aggr>
. 您可以使用-v aggr=<aggr>
语法提供参数。 You will enter that on command line: 您将在命令行中输入:
awk -f script.awk -v aggr=aggr1 -v total=152 tabfile
here is an awk script: 这是一个awk脚本:
BEGIN {
if ( (aggr == "") || (total == 0.) ) {
print "no <aggr> or no <total> arg\n"
print "usage: awk -f script.awk -v aggr=<aggr> -v total=<total> <file_data>"
exit 1;}
sum = 0;
}
$0 ~ aggr {
scurrent = $6; sub("GB","", scurrent);
sum += scurrent;
if (sum <= total) print $2 "\t" $3 "\t" $6;
else exit 0;
}
The BEGIN block is interpreted once, at the beginning! 开始时,BEGIN块将被解释一次! Here you initialize sum variable and you check the presence of mandatory arguments. 在这里,您可以初始化sum变量,并检查强制性参数的存在。 If they are missing, their value is null. 如果缺少它们,则其值为null。
The script will read the file line by line, and will process only lines containing aggr argument. 该脚本将逐行读取文件,并且仅处理包含aggr参数的行。
Each column is referred thanks to $ and its NUM; 每列均通过$及其NUM来引用; your volume size is in the column $6
. 您的交易量大小在$6
栏中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.