Calling an array inside awk to create a table with fixed-width columns

Clash Royale CLAN TAG#URR8PPP
up vote
5
down vote
favorite
I would like to extract data from a file and organize it in a big fixed-widths table. I can expect that this table will have multiple columns, let's say 30 columns. If I create this table using the traditional awk command line, then I will need to write a very long awk command line, something similar to the following:
awk 'printf "%-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5sn", $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30'
Is there anyway to make this linear shorter? For example, I am thinking of implementing an array inside the previous long command. This array will tell awk what are the numbers and the widths of the columns that I would like to create, instead of defining each column separately, something like:
awk 'BEGIN for i in 1..30; do echo %-5sn print i
How can I implement that correctly inside awk to create multiple fixed-widths columns?
awk columns
add a comment |Â
up vote
5
down vote
favorite
I would like to extract data from a file and organize it in a big fixed-widths table. I can expect that this table will have multiple columns, let's say 30 columns. If I create this table using the traditional awk command line, then I will need to write a very long awk command line, something similar to the following:
awk 'printf "%-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5sn", $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30'
Is there anyway to make this linear shorter? For example, I am thinking of implementing an array inside the previous long command. This array will tell awk what are the numbers and the widths of the columns that I would like to create, instead of defining each column separately, something like:
awk 'BEGIN for i in 1..30; do echo %-5sn print i
How can I implement that correctly inside awk to create multiple fixed-widths columns?
awk columns
add a comment |Â
up vote
5
down vote
favorite
up vote
5
down vote
favorite
I would like to extract data from a file and organize it in a big fixed-widths table. I can expect that this table will have multiple columns, let's say 30 columns. If I create this table using the traditional awk command line, then I will need to write a very long awk command line, something similar to the following:
awk 'printf "%-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5sn", $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30'
Is there anyway to make this linear shorter? For example, I am thinking of implementing an array inside the previous long command. This array will tell awk what are the numbers and the widths of the columns that I would like to create, instead of defining each column separately, something like:
awk 'BEGIN for i in 1..30; do echo %-5sn print i
How can I implement that correctly inside awk to create multiple fixed-widths columns?
awk columns
I would like to extract data from a file and organize it in a big fixed-widths table. I can expect that this table will have multiple columns, let's say 30 columns. If I create this table using the traditional awk command line, then I will need to write a very long awk command line, something similar to the following:
awk 'printf "%-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5sn", $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30'
Is there anyway to make this linear shorter? For example, I am thinking of implementing an array inside the previous long command. This array will tell awk what are the numbers and the widths of the columns that I would like to create, instead of defining each column separately, something like:
awk 'BEGIN for i in 1..30; do echo %-5sn print i
How can I implement that correctly inside awk to create multiple fixed-widths columns?
awk columns
awk columns
edited 55 mins ago
asked 3 hours ago
Goro
2,55141849
2,55141849
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
8
down vote
accepted
You can do the print, itself, inside a loop, one field at a time.
awk 'for(i=1;i<=NF;i++) printf "%-5s",$i ; printf("n"); '
Note the printing of the newline is needed after the loop to prevent multiple lines all merging into one.
e.g
echo a b c 32 87 x5 | awk 'for(i=1;i<=NF;i++) printf "%-5s",$i ; printf("n"); '
a b c 32 87 x5
Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
â Goro
1 hour ago
1
Before theforloop you could do something likesum += $27and then at the very end you can use something likeEND print sumto print it out
â Stephen Harris
1 hour ago
add a comment |Â
up vote
2
down vote
You could (but I do not recommend this) build some vars in steps (an example in bash):
$ printf -v l '%s ' 1..30 # list of numbers to use
$ printf -v a '%.0s%%-5s ' $l # make a string of repeated "%-5s"
$ printf -v b ',$%s' $l # make string of field numbers as "$1,$2.."
$ awk -va="$a" 'printf a "n"'"$b"'' infile4
But you could also do it all inside awk:
$ awk 'split($0,a); for(i in a)printf "%-5s", $i; print ""' infile
- The
splitinawkwill use the same regex inFSas used to split the line into fields and place each value in the arraya. - The
forwill (auto) loop over all fields. - The
printfwill print all fields with the same format. - And, the final
printwill place a newline at the end of the line.
This is more flexible as it will work for any amount of fields, even lines with varying number of fields. And is complete inside only one language (easier to understand and maintain).
Or even:
$ awk 'for(i=1;i<=NF;i++) printf("%-5s",$i) ; print ""}' infile
You may change the format to %-5.5s to cut fields that are longer than 5 characters.
Note that awk's printf incorrectly counts decomposed character like eÃÂ as two characters. It seems to count Unicode code points (a common issue) instead of Unicode clusters.
EDIT
Answering this additional question from the comments:
sum the rows that define column #27 in my table
Just add the needed code:
$ awk 'split($0,a);
sum=sum+a[27];
for(i in a) printf "%-5s", $i ;
print ""
END
print "Sum of column 27 is =", sum
' infile
Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
â Goro
1 hour ago
I wish I can give you two thumb-up ;-) thanks again!
â Goro
1 hour ago
Hey, ... You could still select this answer as the accepted one ! :-)
â Isaac
1 hour ago
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
accepted
You can do the print, itself, inside a loop, one field at a time.
awk 'for(i=1;i<=NF;i++) printf "%-5s",$i ; printf("n"); '
Note the printing of the newline is needed after the loop to prevent multiple lines all merging into one.
e.g
echo a b c 32 87 x5 | awk 'for(i=1;i<=NF;i++) printf "%-5s",$i ; printf("n"); '
a b c 32 87 x5
Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
â Goro
1 hour ago
1
Before theforloop you could do something likesum += $27and then at the very end you can use something likeEND print sumto print it out
â Stephen Harris
1 hour ago
add a comment |Â
up vote
8
down vote
accepted
You can do the print, itself, inside a loop, one field at a time.
awk 'for(i=1;i<=NF;i++) printf "%-5s",$i ; printf("n"); '
Note the printing of the newline is needed after the loop to prevent multiple lines all merging into one.
e.g
echo a b c 32 87 x5 | awk 'for(i=1;i<=NF;i++) printf "%-5s",$i ; printf("n"); '
a b c 32 87 x5
Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
â Goro
1 hour ago
1
Before theforloop you could do something likesum += $27and then at the very end you can use something likeEND print sumto print it out
â Stephen Harris
1 hour ago
add a comment |Â
up vote
8
down vote
accepted
up vote
8
down vote
accepted
You can do the print, itself, inside a loop, one field at a time.
awk 'for(i=1;i<=NF;i++) printf "%-5s",$i ; printf("n"); '
Note the printing of the newline is needed after the loop to prevent multiple lines all merging into one.
e.g
echo a b c 32 87 x5 | awk 'for(i=1;i<=NF;i++) printf "%-5s",$i ; printf("n"); '
a b c 32 87 x5
You can do the print, itself, inside a loop, one field at a time.
awk 'for(i=1;i<=NF;i++) printf "%-5s",$i ; printf("n"); '
Note the printing of the newline is needed after the loop to prevent multiple lines all merging into one.
e.g
echo a b c 32 87 x5 | awk 'for(i=1;i<=NF;i++) printf "%-5s",$i ; printf("n"); '
a b c 32 87 x5
answered 3 hours ago
Stephen Harris
21.6k23973
21.6k23973
Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
â Goro
1 hour ago
1
Before theforloop you could do something likesum += $27and then at the very end you can use something likeEND print sumto print it out
â Stephen Harris
1 hour ago
add a comment |Â
Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
â Goro
1 hour ago
1
Before theforloop you could do something likesum += $27and then at the very end you can use something likeEND print sumto print it out
â Stephen Harris
1 hour ago
Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
â Goro
1 hour ago
Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
â Goro
1 hour ago
1
1
Before the
for loop you could do something like sum += $27 and then at the very end you can use something like END print sum to print it outâ Stephen Harris
1 hour ago
Before the
for loop you could do something like sum += $27 and then at the very end you can use something like END print sum to print it outâ Stephen Harris
1 hour ago
add a comment |Â
up vote
2
down vote
You could (but I do not recommend this) build some vars in steps (an example in bash):
$ printf -v l '%s ' 1..30 # list of numbers to use
$ printf -v a '%.0s%%-5s ' $l # make a string of repeated "%-5s"
$ printf -v b ',$%s' $l # make string of field numbers as "$1,$2.."
$ awk -va="$a" 'printf a "n"'"$b"'' infile4
But you could also do it all inside awk:
$ awk 'split($0,a); for(i in a)printf "%-5s", $i; print ""' infile
- The
splitinawkwill use the same regex inFSas used to split the line into fields and place each value in the arraya. - The
forwill (auto) loop over all fields. - The
printfwill print all fields with the same format. - And, the final
printwill place a newline at the end of the line.
This is more flexible as it will work for any amount of fields, even lines with varying number of fields. And is complete inside only one language (easier to understand and maintain).
Or even:
$ awk 'for(i=1;i<=NF;i++) printf("%-5s",$i) ; print ""}' infile
You may change the format to %-5.5s to cut fields that are longer than 5 characters.
Note that awk's printf incorrectly counts decomposed character like eÃÂ as two characters. It seems to count Unicode code points (a common issue) instead of Unicode clusters.
EDIT
Answering this additional question from the comments:
sum the rows that define column #27 in my table
Just add the needed code:
$ awk 'split($0,a);
sum=sum+a[27];
for(i in a) printf "%-5s", $i ;
print ""
END
print "Sum of column 27 is =", sum
' infile
Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
â Goro
1 hour ago
I wish I can give you two thumb-up ;-) thanks again!
â Goro
1 hour ago
Hey, ... You could still select this answer as the accepted one ! :-)
â Isaac
1 hour ago
add a comment |Â
up vote
2
down vote
You could (but I do not recommend this) build some vars in steps (an example in bash):
$ printf -v l '%s ' 1..30 # list of numbers to use
$ printf -v a '%.0s%%-5s ' $l # make a string of repeated "%-5s"
$ printf -v b ',$%s' $l # make string of field numbers as "$1,$2.."
$ awk -va="$a" 'printf a "n"'"$b"'' infile4
But you could also do it all inside awk:
$ awk 'split($0,a); for(i in a)printf "%-5s", $i; print ""' infile
- The
splitinawkwill use the same regex inFSas used to split the line into fields and place each value in the arraya. - The
forwill (auto) loop over all fields. - The
printfwill print all fields with the same format. - And, the final
printwill place a newline at the end of the line.
This is more flexible as it will work for any amount of fields, even lines with varying number of fields. And is complete inside only one language (easier to understand and maintain).
Or even:
$ awk 'for(i=1;i<=NF;i++) printf("%-5s",$i) ; print ""}' infile
You may change the format to %-5.5s to cut fields that are longer than 5 characters.
Note that awk's printf incorrectly counts decomposed character like eÃÂ as two characters. It seems to count Unicode code points (a common issue) instead of Unicode clusters.
EDIT
Answering this additional question from the comments:
sum the rows that define column #27 in my table
Just add the needed code:
$ awk 'split($0,a);
sum=sum+a[27];
for(i in a) printf "%-5s", $i ;
print ""
END
print "Sum of column 27 is =", sum
' infile
Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
â Goro
1 hour ago
I wish I can give you two thumb-up ;-) thanks again!
â Goro
1 hour ago
Hey, ... You could still select this answer as the accepted one ! :-)
â Isaac
1 hour ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
You could (but I do not recommend this) build some vars in steps (an example in bash):
$ printf -v l '%s ' 1..30 # list of numbers to use
$ printf -v a '%.0s%%-5s ' $l # make a string of repeated "%-5s"
$ printf -v b ',$%s' $l # make string of field numbers as "$1,$2.."
$ awk -va="$a" 'printf a "n"'"$b"'' infile4
But you could also do it all inside awk:
$ awk 'split($0,a); for(i in a)printf "%-5s", $i; print ""' infile
- The
splitinawkwill use the same regex inFSas used to split the line into fields and place each value in the arraya. - The
forwill (auto) loop over all fields. - The
printfwill print all fields with the same format. - And, the final
printwill place a newline at the end of the line.
This is more flexible as it will work for any amount of fields, even lines with varying number of fields. And is complete inside only one language (easier to understand and maintain).
Or even:
$ awk 'for(i=1;i<=NF;i++) printf("%-5s",$i) ; print ""}' infile
You may change the format to %-5.5s to cut fields that are longer than 5 characters.
Note that awk's printf incorrectly counts decomposed character like eÃÂ as two characters. It seems to count Unicode code points (a common issue) instead of Unicode clusters.
EDIT
Answering this additional question from the comments:
sum the rows that define column #27 in my table
Just add the needed code:
$ awk 'split($0,a);
sum=sum+a[27];
for(i in a) printf "%-5s", $i ;
print ""
END
print "Sum of column 27 is =", sum
' infile
You could (but I do not recommend this) build some vars in steps (an example in bash):
$ printf -v l '%s ' 1..30 # list of numbers to use
$ printf -v a '%.0s%%-5s ' $l # make a string of repeated "%-5s"
$ printf -v b ',$%s' $l # make string of field numbers as "$1,$2.."
$ awk -va="$a" 'printf a "n"'"$b"'' infile4
But you could also do it all inside awk:
$ awk 'split($0,a); for(i in a)printf "%-5s", $i; print ""' infile
- The
splitinawkwill use the same regex inFSas used to split the line into fields and place each value in the arraya. - The
forwill (auto) loop over all fields. - The
printfwill print all fields with the same format. - And, the final
printwill place a newline at the end of the line.
This is more flexible as it will work for any amount of fields, even lines with varying number of fields. And is complete inside only one language (easier to understand and maintain).
Or even:
$ awk 'for(i=1;i<=NF;i++) printf("%-5s",$i) ; print ""}' infile
You may change the format to %-5.5s to cut fields that are longer than 5 characters.
Note that awk's printf incorrectly counts decomposed character like eÃÂ as two characters. It seems to count Unicode code points (a common issue) instead of Unicode clusters.
EDIT
Answering this additional question from the comments:
sum the rows that define column #27 in my table
Just add the needed code:
$ awk 'split($0,a);
sum=sum+a[27];
for(i in a) printf "%-5s", $i ;
print ""
END
print "Sum of column 27 is =", sum
' infile
edited 1 hour ago
answered 1 hour ago
Isaac
7,0721834
7,0721834
Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
â Goro
1 hour ago
I wish I can give you two thumb-up ;-) thanks again!
â Goro
1 hour ago
Hey, ... You could still select this answer as the accepted one ! :-)
â Isaac
1 hour ago
add a comment |Â
Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
â Goro
1 hour ago
I wish I can give you two thumb-up ;-) thanks again!
â Goro
1 hour ago
Hey, ... You could still select this answer as the accepted one ! :-)
â Isaac
1 hour ago
Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
â Goro
1 hour ago
Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
â Goro
1 hour ago
I wish I can give you two thumb-up ;-) thanks again!
â Goro
1 hour ago
I wish I can give you two thumb-up ;-) thanks again!
â Goro
1 hour ago
Hey, ... You could still select this answer as the accepted one ! :-)
â Isaac
1 hour ago
Hey, ... You could still select this answer as the accepted one ! :-)
â Isaac
1 hour ago
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f469550%2fcalling-an-array-inside-awk-to-create-a-table-with-fixed-width-columns%23new-answer', 'question_page');
);
Post as a guest
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
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
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
