Kornシェルスクリプトによって生成された出力ファイルがあります。出力を空白に均等に揃える必要があります。残念ながら、column -t
AIXでは使用できません。
物理ファイル:
X1vir1000 x1-DFB127 x1ttcb101_mv_03_2016 Not-activated 16 x1ttcr181 x1ttcr182
X1vir1000 x1-DFB127 x1dvdb201_mv_pmp-132 Not-activated 3 x1ttcr181 x1ttcr182
X3vir1000 x3-DFB116 x3dvdb202_mv_05032016 Not-activated 22 x3ttcr175 x3ttcr176
X3vir1000 x3-DFB116 x3ttcb203_mv_03_2016 Not-activated 19 x3ttcr175 x3ttcr176
X3vir1000 x3-DFB116 x3trcb223_mv_10_2017 Not-activated 29 x3ttcr175 x3ttcr176
X3vir1000 x3-DFB117 x3trvf245_mv_08_2017 Not-activated 27 x3ttcr177 x3ttcr178
X3vir1000 x3-DFB131 CR74536_x3dvap234_Decom Not-activated 101 x3ttcr213 x3ttcr214
X3vir1000 x3-DFB132 decommissioned_x3trcb223 Not-activated 138 x3ttcr217 x3ttcr218
X3vir1000 x3-DFB132 decommissioned_x3trcb312 Not-activated 116 x3ttcr217 x3ttcr218
X9vir1000 x9-DFB005-8233-E8B-SN1030BDR x9trcb003_vhost1_decomm Not-activated 4 x9ttcr012 x9ttcr013
Expected Result:
X1vir1000 x1-DFB127 x1ttcb101_mv_03_2016 Not-activated 16 x1ttcr181 x1ttcr182
X1vir1000 x1-DFB127 x1dvdb201_mv_pmp-132 Not-activated 3 x1ttcr181 x1ttcr182
X3vir1000 x3-DFB116 x3dvdb202_mv_05032016 Not-activated 22 x3ttcr175 x3ttcr176
X3vir1000 x3-DFB116 x3ttcb203_mv_03_2016 Not-activated 19 x3ttcr175 x3ttcr176
X3vir1000 x3-DFB116 x3trcb223_mv_10_2017 Not-activated 29 x3ttcr175 x3ttcr176
X3vir1000 x3-DFB117 x3trvf245_mv_08_2017 Not-activated 27 x3ttcr177 x3ttcr178
X3vir1000 x3-DFB131 CR74536_x3dvap234_Decom Not-activated 101 x3ttcr213 x3ttcr214
X3vir1000 x3-DFB132 decommissioned_x3trcb223 Not-activated 138 x3ttcr217 x3ttcr218
X3vir1000 x3-DFB132 decommissioned_x3trcb312 Not-activated 116 x3ttcr217 x3ttcr218
X9vir1000 x9-DFB005-8233-E8B-SN1030BDR x9trcb003_vhost1_decomm Not-activated 4 x9ttcr012 x9ttcr013
答え1
FNR == NR {
for (i = 1; i <= NF; ++i) {
width = length($i)
maxwidth[i] = (width > maxwidth[i] ? width : maxwidth[i])
}
next
}
{
for (i = 1; i <= NF; ++i)
printf("%-*s%s", maxwidth[i], $i,
(i == NF ? "\n" : (delim == "" ? " " : delim)) )
}
スクリプトawk
は同じファイルを2回読み取ることを期待しています。最初は、入力データの各列の最大幅を記録します。 2番目に、対応する最大幅で書式設定された列を印刷します。
この変数を設定すると列をdelim
区切るために使用され、それ以外の場合は空白文字が使用されます。
デフォルトでは、生データはスペースで区切られていると見なされます。タブで区切られている場合は、-F '\t'
コマンドラインで使用してください。
与えられたデータに対してテストを2回実行します。 (入力ファイルを指定する必要があります。)二重):
$ awk -f ./script.awk file file
X1vir1000 x1-DFB127 x1ttcb101_mv_03_2016 Not-activated 16 x1ttcr181 x1ttcr182
X1vir1000 x1-DFB127 x1dvdb201_mv_pmp-132 Not-activated 3 x1ttcr181 x1ttcr182
X3vir1000 x3-DFB116 x3dvdb202_mv_05032016 Not-activated 22 x3ttcr175 x3ttcr176
X3vir1000 x3-DFB116 x3ttcb203_mv_03_2016 Not-activated 19 x3ttcr175 x3ttcr176
X3vir1000 x3-DFB116 x3trcb223_mv_10_2017 Not-activated 29 x3ttcr175 x3ttcr176
X3vir1000 x3-DFB117 x3trvf245_mv_08_2017 Not-activated 27 x3ttcr177 x3ttcr178
X3vir1000 x3-DFB131 CR74536_x3dvap234_Decom Not-activated 101 x3ttcr213 x3ttcr214
X3vir1000 x3-DFB132 decommissioned_x3trcb223 Not-activated 138 x3ttcr217 x3ttcr218
X3vir1000 x3-DFB132 decommissioned_x3trcb312 Not-activated 116 x3ttcr217 x3ttcr218
X9vir1000 x9-DFB005-8233-E8B-SN1030BDR x9trcb003_vhost1_decomm Not-activated 4 x9ttcr012 x9ttcr013
$ awk -v delim=' | ' -f ./script.awk file file
X1vir1000 | x1-DFB127 | x1ttcb101_mv_03_2016 | Not-activated | 16 | x1ttcr181 | x1ttcr182
X1vir1000 | x1-DFB127 | x1dvdb201_mv_pmp-132 | Not-activated | 3 | x1ttcr181 | x1ttcr182
X3vir1000 | x3-DFB116 | x3dvdb202_mv_05032016 | Not-activated | 22 | x3ttcr175 | x3ttcr176
X3vir1000 | x3-DFB116 | x3ttcb203_mv_03_2016 | Not-activated | 19 | x3ttcr175 | x3ttcr176
X3vir1000 | x3-DFB116 | x3trcb223_mv_10_2017 | Not-activated | 29 | x3ttcr175 | x3ttcr176
X3vir1000 | x3-DFB117 | x3trvf245_mv_08_2017 | Not-activated | 27 | x3ttcr177 | x3ttcr178
X3vir1000 | x3-DFB131 | CR74536_x3dvap234_Decom | Not-activated | 101 | x3ttcr213 | x3ttcr214
X3vir1000 | x3-DFB132 | decommissioned_x3trcb223 | Not-activated | 138 | x3ttcr217 | x3ttcr218
X3vir1000 | x3-DFB132 | decommissioned_x3trcb312 | Not-activated | 116 | x3ttcr217 | x3ttcr218
X9vir1000 | x9-DFB005-8233-E8B-SN1030BDR | x9trcb003_vhost1_decomm | Not-activated | 4 | x9ttcr012 | x9ttcr013
awk
上記のプログラムを含み、2つのオプションを使用するシェルスクリプト:
-d delim
delim
使用する出力区切り記号はどこにありますか?-D delim
delim
使用する入力区切り記号はどこにあります(-D '\t'
入力データのタブ文字など)。
スクリプトは、次のように上記の2つの実行を再作成するために使用されます。
./script.sh file
./script.sh -d ' | ' file
スクリプト:
#!/bin/sh
while getopts 'd:D:' opt; do
case $opt in
d) delim=$OPTARG ;;
D) fs=$OPTARG ;;
*) echo 'Error in command line parsing' >&2
exit 1
esac
done
shift "$(( OPTIND - 1 ))"
tmpfile=$(mktemp)
# If mktemp is not available:
# tmpfile="${TMPDIR:-/tmp}/columnn-t.tmp"
# ... or something similar
cat "$1" >$tmpfile
awk ${delim:+-v delim="$delim"} ${fs:+-F "$fs"} '
FNR == NR {
for (i = 1; i <= NF; ++i) {
width = length($i)
maxwidth[i] = (width > maxwidth[i] ? width : maxwidth[i])
}
next
}
{
for (i = 1; i <= NF; ++i)
printf("%-*s%s", maxwidth[i], $i,
(i == NF ? "\n" : (delim == "" ? " " : delim)) )
}' "$tmpfile" "$tmpfile"
rm -f "$tmpfile"