[英]Use awk to print first column, second column and one column every four after that
[英]awk: delete the first column and add four columns after the last one
我有一個文件,數據表示為:
32446191 0 5 5 2014-06-27 13210
62357877 18 89 89 2014-06-27 13210
33879626 81 1 1 2014-06-27 13210
實際上,我大約有30列。 我需要刪除第一列(和分隔符),並在最后一列之后添加四列。 然后結果將是:
0 5 5 2014-06-27 13210 0 0 0 0
18 89 89 2014-06-27 13210 0 0 0 0
81 1 1 2014-06-27 13210 0 0 0 0
如何使用一個命令在awk中執行此操作? 感謝您的幫助。
保留原始間距:
$ awk -v OFS=' ' '{sub(/[^[:space:]]+[[:space:]]+/,""); print $0, 0, 0, 0, 0}' file
0 5 5 2014-06-27 13210 0 0 0 0
18 89 89 2014-06-27 13210 0 0 0 0
81 1 1 2014-06-27 13210 0 0 0 0
調整OFS
以使其適合。
awk '{printf("%-4s%-4s%-4s%-12s%-5s 0 0 0 0\n",$2,$3,$4,$5,$6)}' file
awk '{for(i=2;i<=NF;++i) printf("%-4s ", $i); printf " 0 0 0 0\n"} file
只需打印帶有額外0的列,然后使用column -t
:
awk '{ print $2, $3, $4, $5, $6, 0, 0, 0, 0 }' file | column -t
輸出:
0 5 5 2014-06-27 13210 0 0 0 0
18 89 89 2014-06-27 13210 0 0 0 0
81 1 1 2014-06-27 13210 0 0 0 0
對於自定義列:
awk -v columns='2 3 4 5 6' 'BEGIN { l = split(columns, c); } { t = $c[1]; for (i = 2; i <= l; ++i) t = t OFS $c[i]; print t, 0, 0, 0, 0; t = "" }' file
修改OFS
,使用printf
,或簡單地使用column -t
進行格式化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.