Align columns evenly without “column - t”
up vote
1
down vote
favorite
I have an output file created from a Korn shell script. I need to align the output with spaces evenly. Unfortunately column -t
is not available on AIX.
Actual 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
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
shell-script awk sed ksh aix
add a comment |
up vote
1
down vote
favorite
I have an output file created from a Korn shell script. I need to align the output with spaces evenly. Unfortunately column -t
is not available on AIX.
Actual 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
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
shell-script awk sed ksh aix
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have an output file created from a Korn shell script. I need to align the output with spaces evenly. Unfortunately column -t
is not available on AIX.
Actual 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
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
shell-script awk sed ksh aix
I have an output file created from a Korn shell script. I need to align the output with spaces evenly. Unfortunately column -t
is not available on AIX.
Actual 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
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
shell-script awk sed ksh aix
shell-script awk sed ksh aix
edited Nov 29 at 7:53
Kusalananda
118k16223364
118k16223364
asked Nov 27 at 8:12
satsensort
248
248
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
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)) )
}
This awk
script expects to read the same file twice. The first time, it records the maximum width of each column in the input data. The second time, it prints the columns formatted to that maximum width.
If the delim
variable is set, it is used to delimit the columns, otherwise a space character is used.
The default is to assume that the original data is whitespace-delimited. If it is tab-delimited, use -F 't'
on the command line.
Two test runs on the given data (note that the input file has to be specified twice):
$ 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
A shell script that embeds the above awk
program and takes two options:
-d delim
wheredelim
is the output delimiter to use.
-D delim
wheredelim
is the input delimiter to use (e.g.-D 't'
for tabs in the input data).
The script would be use like this to recreate the two runs above:
./script.sh file
./script.sh -d ' | ' file
The script:
#!/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"
Many thanks @kusalananda. The awk script helped in aligning the text
– satsensort
Nov 27 at 9:55
1
Ifi == NF
, you can useORS
instead of""
, and thus remove explicitly printing a newline.
– glenn jackman
Nov 27 at 15:59
@glennjackman Thanks, didn't spot that when I wrote it.
– Kusalananda
Nov 29 at 7:48
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
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)) )
}
This awk
script expects to read the same file twice. The first time, it records the maximum width of each column in the input data. The second time, it prints the columns formatted to that maximum width.
If the delim
variable is set, it is used to delimit the columns, otherwise a space character is used.
The default is to assume that the original data is whitespace-delimited. If it is tab-delimited, use -F 't'
on the command line.
Two test runs on the given data (note that the input file has to be specified twice):
$ 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
A shell script that embeds the above awk
program and takes two options:
-d delim
wheredelim
is the output delimiter to use.
-D delim
wheredelim
is the input delimiter to use (e.g.-D 't'
for tabs in the input data).
The script would be use like this to recreate the two runs above:
./script.sh file
./script.sh -d ' | ' file
The script:
#!/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"
Many thanks @kusalananda. The awk script helped in aligning the text
– satsensort
Nov 27 at 9:55
1
Ifi == NF
, you can useORS
instead of""
, and thus remove explicitly printing a newline.
– glenn jackman
Nov 27 at 15:59
@glennjackman Thanks, didn't spot that when I wrote it.
– Kusalananda
Nov 29 at 7:48
add a comment |
up vote
2
down vote
accepted
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)) )
}
This awk
script expects to read the same file twice. The first time, it records the maximum width of each column in the input data. The second time, it prints the columns formatted to that maximum width.
If the delim
variable is set, it is used to delimit the columns, otherwise a space character is used.
The default is to assume that the original data is whitespace-delimited. If it is tab-delimited, use -F 't'
on the command line.
Two test runs on the given data (note that the input file has to be specified twice):
$ 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
A shell script that embeds the above awk
program and takes two options:
-d delim
wheredelim
is the output delimiter to use.
-D delim
wheredelim
is the input delimiter to use (e.g.-D 't'
for tabs in the input data).
The script would be use like this to recreate the two runs above:
./script.sh file
./script.sh -d ' | ' file
The script:
#!/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"
Many thanks @kusalananda. The awk script helped in aligning the text
– satsensort
Nov 27 at 9:55
1
Ifi == NF
, you can useORS
instead of""
, and thus remove explicitly printing a newline.
– glenn jackman
Nov 27 at 15:59
@glennjackman Thanks, didn't spot that when I wrote it.
– Kusalananda
Nov 29 at 7:48
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
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)) )
}
This awk
script expects to read the same file twice. The first time, it records the maximum width of each column in the input data. The second time, it prints the columns formatted to that maximum width.
If the delim
variable is set, it is used to delimit the columns, otherwise a space character is used.
The default is to assume that the original data is whitespace-delimited. If it is tab-delimited, use -F 't'
on the command line.
Two test runs on the given data (note that the input file has to be specified twice):
$ 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
A shell script that embeds the above awk
program and takes two options:
-d delim
wheredelim
is the output delimiter to use.
-D delim
wheredelim
is the input delimiter to use (e.g.-D 't'
for tabs in the input data).
The script would be use like this to recreate the two runs above:
./script.sh file
./script.sh -d ' | ' file
The script:
#!/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"
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)) )
}
This awk
script expects to read the same file twice. The first time, it records the maximum width of each column in the input data. The second time, it prints the columns formatted to that maximum width.
If the delim
variable is set, it is used to delimit the columns, otherwise a space character is used.
The default is to assume that the original data is whitespace-delimited. If it is tab-delimited, use -F 't'
on the command line.
Two test runs on the given data (note that the input file has to be specified twice):
$ 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
A shell script that embeds the above awk
program and takes two options:
-d delim
wheredelim
is the output delimiter to use.
-D delim
wheredelim
is the input delimiter to use (e.g.-D 't'
for tabs in the input data).
The script would be use like this to recreate the two runs above:
./script.sh file
./script.sh -d ' | ' file
The script:
#!/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"
edited Nov 29 at 7:58
answered Nov 27 at 9:06
Kusalananda
118k16223364
118k16223364
Many thanks @kusalananda. The awk script helped in aligning the text
– satsensort
Nov 27 at 9:55
1
Ifi == NF
, you can useORS
instead of""
, and thus remove explicitly printing a newline.
– glenn jackman
Nov 27 at 15:59
@glennjackman Thanks, didn't spot that when I wrote it.
– Kusalananda
Nov 29 at 7:48
add a comment |
Many thanks @kusalananda. The awk script helped in aligning the text
– satsensort
Nov 27 at 9:55
1
Ifi == NF
, you can useORS
instead of""
, and thus remove explicitly printing a newline.
– glenn jackman
Nov 27 at 15:59
@glennjackman Thanks, didn't spot that when I wrote it.
– Kusalananda
Nov 29 at 7:48
Many thanks @kusalananda. The awk script helped in aligning the text
– satsensort
Nov 27 at 9:55
Many thanks @kusalananda. The awk script helped in aligning the text
– satsensort
Nov 27 at 9:55
1
1
If
i == NF
, you can use ORS
instead of ""
, and thus remove explicitly printing a newline.– glenn jackman
Nov 27 at 15:59
If
i == NF
, you can use ORS
instead of ""
, and thus remove explicitly printing a newline.– glenn jackman
Nov 27 at 15:59
@glennjackman Thanks, didn't spot that when I wrote it.
– Kusalananda
Nov 29 at 7:48
@glennjackman Thanks, didn't spot that when I wrote it.
– Kusalananda
Nov 29 at 7:48
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484390%2falign-columns-evenly-without-column-t%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown