오래 전에 나는 입력을 멋진 형식의 테이블로 만드는 명령을 사용하는 것을 기억합니다.
예를 들어이 입력의 경우
apple 1 100
orange 20 19
pineapple 1000 87
avocado 4 30
출력은 다음과 유사합니다.
apple 1 100
orange 20 19
pineapple 1000 87
avocado 4 30
이 도구의 이름을 알고 싶습니다.
답변
사용하십시오 column -t
. column 은 util-linux의 일부입니다 .
$ column -t <<END
> apple 1 100
> orange 20 19
> pineapple 1000 87
> avocado 4 30
> END
apple 1 100
orange 20 19
pineapple 1000 87
avocado 4 30
답변
awk
stdin을 다루는 솔루션
이후 column
POSIX 아니라, 어쩌면이 있습니다 :
mycolumn() (
file="${1:--}"
if [ "$file" = - ]; then
file="$(mktemp)"
cat >"${file}"
fi
awk '
FNR == 1 { if (NR == FNR) next }
NR == FNR {
for (i = 1; i <= NF; i++) {
l = length($i)
if (w[i] < l)
w[i] = l
}
next
}
{
for (i = 1; i <= NF; i++)
printf "%*s", w[i] + (i > 1 ? 1 : 0), $i
print ""
}
' "$file" "$file"
if [ "$file" = - ]; then
rm "$file"
fi
)
테스트:
printf '12 1234 1
12345678 1 123
1234 123456 123456
' > file
테스트 명령 :
mycolumn file
mycolumn <file
mycolumn - <file
모두를위한 출력 :
12 1234 1
12345678 1 123
1234 123456 123456
참조 :
답변
상대적으로 작은 파일 (길이가 바이트보다 작은 경우 getconf ARG_MAX
)과 입력 크기가 알려지지 않은 경우 (예를 들어 과일 이름이 18 자보다 길지 않음) printf
유용한 bash
예는 다음과 같습니다.
printf '%-20s %5s %5s\n' $(<file.txt)
산출:
apple 1 100
orange 20 19
pineapple 1000 87
avocado 4 30
숫자가되는 방법을 참고 바로 정당화.