![如何编写bash脚本动态打印行和列数据并更新同一文件中的相同数据?](https://linux22.com/image/92457/%E5%A6%82%E4%BD%95%E7%BC%96%E5%86%99bash%E8%84%9A%E6%9C%AC%E5%8A%A8%E6%80%81%E6%89%93%E5%8D%B0%E8%A1%8C%E5%92%8C%E5%88%97%E6%95%B0%E6%8D%AE%E5%B9%B6%E6%9B%B4%E6%96%B0%E5%90%8C%E4%B8%80%E6%96%87%E4%BB%B6%E4%B8%AD%E7%9A%84%E7%9B%B8%E5%90%8C%E6%95%B0%E6%8D%AE%EF%BC%9F%20.png)
abc_hosts,pwd_host_id,pwd_host_id,主机名,ddd_status,dddd_status,
start_hosts,,,,,,,,,,,,,,,,,,,,1,o1,fhffhfh,1,1,fff,fdfd,172.33.33.33,172.30.30.12,172.30.30.11,oreere.dff ,43,443343,1111,43435,1099,43434443444,3232321312312 end_hosts,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,, abc_hosts,pwd_host_id,pwd_host_id,主机名,ddd_status,dddd_status,
start_hosts2,,,,,,,,,,,,,,,,,,,,1,o1,fhffhfh,1,1,fff,fdfd,172.33.33.33,172.30.30.12,172.30.30.11,oreere.dff ,43,443343,1111,43435,1099,43434443444,3232321312312 end_hosts2,,,,,,,,,,,,,,,,,,,,
答案1
这个awk
脚本应该可以帮助您开始:
BEGIN { FS = "," }
$1 {
if ($1 == "end_" tablename) {
exit 0;
} else if ($1 == "start_" tablename) {
in_table = 1;
} else if ($1 == tablename) {
count = split($0, columns);
}
next;
}
in_table {
for (i = 2; i <= NF; i++) {
values[i] = values[i] "," $i;
}
}
END {
for (i = 2; i <= count; i++) {
if (columns[i]) {
print columns[i] " - " substr(values[i], 2);
}
}
}
像这样称呼它:
awk -f config.awk -v tablename=interfaces_setup config.csv