[英]Comparing five columns of two different files and printing the differences of only two columns using awk
[英]Printing columns and their headers using awk
我正在嘗試為表中的列設置標題,我使用了以下代碼:
$ awk 'BEGIN {print "Name\tDescription\tType\tPrice";}
> {print $1,"\t",$2,"\t",$3,"\t",$4,"\t",$NF;}
> END{print "Report Generated\n--------------";
> }' toys | column -s '/' -t
我也試過這段代碼,但我不明白那里的語法和實現:
$ awk 'BEGIN {print "Name\tDescription\tType\tPrice";}
> {printf "%-24s %s\n", $1,$2,$3,$NF;}
> }' toys
我想要的數據和結果:
該表包含:
Mini car Kids under 5 Wireless 2000
Barbie Kids 6 - 12 Electronic 3000
Horse Kids 6 -8 Electronic 4000
Gitar 12 -16 ELectronic 45000
當我打印上面的命令時,它給了我這個 output:
Name Description Type Price
Mini car Kids under 5 Wireless 2000
Barbie Kids 6 - 12 Electronic 3000
Horse Kids 6 -8 Electronic 4000
Gitar 12 -16 ELectronic 45000
我需要幫助打印它們:
Name Description Type Price
Mini car Kids under 5 Wireless 2000
Barbie Kids 6 - 12 Electronic 3000
Horse Kids 6 -8 Electronic 4000
Gitar 12 -16 ELectronic 45000
對於要打印的每個值,您都需要一個格式化運算符。 由於您要打印 4 列,因此需要 4 個格式化運算符來指定每列的寬度。
$ awk 'BEGIN {printf("%-10s %-15s %-10s %s\n", "Name", "Description", "Type", "Price");}
> {printf("%-10s %-15s %-10s %d\n", $1, $2, $3, $4)}
> }' toys
`%s` is for printing strings, `%d` is for printing integers.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.