Bash

如何編寫bash腳本動態列印行和列數據並更新同一文件中的相同數據?

  • September 9, 2016

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, 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 ,,,,,,,,,,,,,,,,,,,,

這個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

引用自:https://unix.stackexchange.com/questions/303527