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
는 동일한 파일을 두 번 읽을 것으로 예상합니다. 처음에는 입력 데이터의 각 열의 최대 너비를 기록합니다. 두 번째로 해당 최대 너비로 형식이 지정된 열을 인쇄합니다.
이 변수를 설정 하면 delim
열을 구분하는 데 사용되고, 그렇지 않으면 공백 문자가 사용됩니다.
기본적으로 원시 데이터는 공백으로 구분된 것으로 간주됩니다. 탭으로 구분된 경우 -F '\t'
명령줄에서 사용하세요.
주어진 데이터에 대해 테스트를 두 번 실행합니다. (입력 파일을 지정해야 합니다.)두 배):
$ 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
위 프로그램을 포함하고 두 가지 옵션을 사용하는 쉘 스크립트:
-d delim
delim
사용할 출력 구분 기호는 어디에 있습니까?-D delim
delim
사용할 입력 구분 기호는 어디에 있습니까(예:-D '\t'
입력 데이터의 탭 문자).
스크립트는 다음과 같이 위의 두 실행을 다시 만드는 데 사용됩니다.
./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"