Modify width of first column in file with a variable number of fields, using awkAwk: expanding first field along the columnsplitting a file by column numberSkip the first 6 lines/rows in a text file with awkUse awk to get two specific columns from third line of file based on value in first lineHow to add column in the beginning of file using perl?How to replace the content of specific column with awk? Tab Delimited FileHow to print all fields containing one of two strings in a table with awkawk - , fixed width columnsHow to sum each column and print column name and column sum using awk?
What is the difference between "mère" and "mère de famille"?
Will Curiosity and the Mars 2020 rover be able to communicate with each other via a Mars orbiter?
Physical interpretation of gamma matrices
Short story from the 70s(?) about aliens/angels destroying humankind, from the point of view of a priest/pastor
Why does this process map every fraction to the golden ratio?
Why is oil used as the lubricant in power generators, while water is the most available, cheapest and accessible lubricant?
Hough transform algorithm - Idiomatic c++
Triangle puzzle
What antenna is this in an Apollo 15 LM photo?
Using a sealant to stop a toilet tank leak
How to end sending data over I2C by Slave or Master?
Lost Time at Motel?
Are silicone socks safe?
My PhD defense is next week and I am having negative thoughts about my work and knowledge. Any advice on how to tackle this?
Is a geodesic in the 4d spacetime still a geodesic after projection onto the 3d space?
Hearts and Spades in a Row
Patent Agreement in Order to Graduate
Why is there potato in meatballs?
Could dinosaurs breathe modern air?
How was the Luftwaffe able to destroy nearly 4000 Soviet aircraft in 3 days of operation Barbarossa?
Is CCing the manager in first e-mail regarding urgent matter considered escalation?
Is my Reactive Step homebrew spell balanced?
Is it possible to protect saved games on Nintendo Switch from being played by other users?
Designing Borders with QGIS
Modify width of first column in file with a variable number of fields, using awk
Awk: expanding first field along the columnsplitting a file by column numberSkip the first 6 lines/rows in a text file with awkUse awk to get two specific columns from third line of file based on value in first lineHow to add column in the beginning of file using perl?How to replace the content of specific column with awk? Tab Delimited FileHow to print all fields containing one of two strings in a table with awkawk - , fixed width columnsHow to sum each column and print column name and column sum using awk?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
I understand how to use awk's printf function, but I don't want to specify every field.
For example, assume this is my file:
c1|c2|c3|c4|c5
c6|c7|c8|c9|c10
c11|c12|c13|c14|c15
I want to format it so that every record's first field is the width of c11 -- the longest cell in the first field:
c1 |c2|c3|c4|c5
c6 |c7|c8|c9|c10
c11|c12|c13|c14|c15
I understand that I could specify:
awk -F"|" 'printf "%-3s%s%s%s%sn", $1, $2, $3, $4, $5' file > newfile
Let's assume I know what I want the width of the first column to be, but I do NOT know how many fields are in the file. Basically I want to do something like:
... '", $1'
... and then print the rest of the fields in their original format.
awk text-formatting printf
add a comment
|
I understand how to use awk's printf function, but I don't want to specify every field.
For example, assume this is my file:
c1|c2|c3|c4|c5
c6|c7|c8|c9|c10
c11|c12|c13|c14|c15
I want to format it so that every record's first field is the width of c11 -- the longest cell in the first field:
c1 |c2|c3|c4|c5
c6 |c7|c8|c9|c10
c11|c12|c13|c14|c15
I understand that I could specify:
awk -F"|" 'printf "%-3s%s%s%s%sn", $1, $2, $3, $4, $5' file > newfile
Let's assume I know what I want the width of the first column to be, but I do NOT know how many fields are in the file. Basically I want to do something like:
... '", $1'
... and then print the rest of the fields in their original format.
awk text-formatting printf
Another way to address it:sed 's/|/'' '' '' |/;s/(...) */1/'
(here adding extra quotes to insert those 3 spaces as the SE comments squeeze contiguous spaces into one)
– Stéphane Chazelas
Sep 11 at 16:23
add a comment
|
I understand how to use awk's printf function, but I don't want to specify every field.
For example, assume this is my file:
c1|c2|c3|c4|c5
c6|c7|c8|c9|c10
c11|c12|c13|c14|c15
I want to format it so that every record's first field is the width of c11 -- the longest cell in the first field:
c1 |c2|c3|c4|c5
c6 |c7|c8|c9|c10
c11|c12|c13|c14|c15
I understand that I could specify:
awk -F"|" 'printf "%-3s%s%s%s%sn", $1, $2, $3, $4, $5' file > newfile
Let's assume I know what I want the width of the first column to be, but I do NOT know how many fields are in the file. Basically I want to do something like:
... '", $1'
... and then print the rest of the fields in their original format.
awk text-formatting printf
I understand how to use awk's printf function, but I don't want to specify every field.
For example, assume this is my file:
c1|c2|c3|c4|c5
c6|c7|c8|c9|c10
c11|c12|c13|c14|c15
I want to format it so that every record's first field is the width of c11 -- the longest cell in the first field:
c1 |c2|c3|c4|c5
c6 |c7|c8|c9|c10
c11|c12|c13|c14|c15
I understand that I could specify:
awk -F"|" 'printf "%-3s%s%s%s%sn", $1, $2, $3, $4, $5' file > newfile
Let's assume I know what I want the width of the first column to be, but I do NOT know how many fields are in the file. Basically I want to do something like:
... '", $1'
... and then print the rest of the fields in their original format.
awk text-formatting printf
awk text-formatting printf
edited Sep 15 at 12:07
Jeff Schaller♦
52.1k11 gold badges76 silver badges172 bronze badges
52.1k11 gold badges76 silver badges172 bronze badges
asked Sep 11 at 16:04
Kayli O'KeefeKayli O'Keefe
1034 bronze badges
1034 bronze badges
Another way to address it:sed 's/|/'' '' '' |/;s/(...) */1/'
(here adding extra quotes to insert those 3 spaces as the SE comments squeeze contiguous spaces into one)
– Stéphane Chazelas
Sep 11 at 16:23
add a comment
|
Another way to address it:sed 's/|/'' '' '' |/;s/(...) */1/'
(here adding extra quotes to insert those 3 spaces as the SE comments squeeze contiguous spaces into one)
– Stéphane Chazelas
Sep 11 at 16:23
Another way to address it:
sed 's/|/'' '' '' |/;s/(...) */1/'
(here adding extra quotes to insert those 3 spaces as the SE comments squeeze contiguous spaces into one)– Stéphane Chazelas
Sep 11 at 16:23
Another way to address it:
sed 's/|/'' '' '' |/;s/(...) */1/'
(here adding extra quotes to insert those 3 spaces as the SE comments squeeze contiguous spaces into one)– Stéphane Chazelas
Sep 11 at 16:23
add a comment
|
4 Answers
4
active
oldest
votes
You can use sprintf
to re-format $1
only.
Ex.
$ awk 'BEGIN" $1 = sprintf("%-3s",$1) 1' file
c1 |c2|c3|c4|c5
c6 |c7|c8|c9|c10
c11|c12|c13|c14|c15
Concise, you can use dynamic formatting with sprintf too: E.g.awk -vf1=3 'BEGIN"$1=sprintf("%-*s",f1,$1)1' test.txt
– A.Danischewski
Sep 11 at 17:36
@A.Danischewski - Well, dang. I've been doing extensive awk programming for ~17 years, and have never come across that one before. To think of all the hassles it would have saved me.
– Paul Sinclair
Sep 12 at 16:47
add a comment
|
To figure out the largest/longest length of the first field, and then to reformat the values in the field according that length, you will have to do two separate passes over the file.
awk 'BEGIN "
FNR == NR if (m < (n=length($1))) m = n; next
$1 = sprintf("%-*s", m, $1); print ' file file
(note that the input file is specified twice on the command line)
For the data that you present, this would produce
c1 |c2|c3|c4|c5
c6 |c7|c8|c9|c10
c11|c12|c13|c14|c15
The first pass is handled by the FNR == NR
block, which simply keeps track of the longest field seen so far (m
contains the maximum length seen), and skips to the next line.
The second pass is handled by the last block, which reformats the first field using sprintf()
. The format string %-*s
means "a left-justified string whose width is given by the integer argument before the argument that holds the actual string".
This could obviously be expanded to do all columns by turning the scalar m
into an array that holds the maximum width of each column:
$ awk 'BEGIN "
FNR == NR for (i=1; i<=NF; ++i) if (m[i] < (n=length($i))) m[i] = n; next
for (i=1; i<=NF; ++i) $i = sprintf("%-*s", m[i], $i); print ' file file
c1 |c2 |c3 |c4 |c5
c6 |c7 |c8 |c9 |c10
c11|c12|c13|c14|c15
add a comment
|
The intelligent way is what steeldriver suggested. The needlessly convoluted way is to iterate over every field:
$ awk -F'|' '",$1; for(i=2;i<NF;i++)",$i printf "%sn", $i' file
c1 |c2|c3|c4|c5
c6 |c7|c8|c9|c10
c11|c12|c13|c14|c15
But just sprintf
$1
and be done with it.
1
You've got it a bit backwards, small concise statements generally are more convoluted. Iterating over the fields is less convoluted.
– A.Danischewski
Sep 11 at 19:40
add a comment
|
In Awk you can use a "*" to generate a dynamic printf format string.
If you know the length already you can pass the field length for the first column with -v.
awk -vcol1=3 'BEGINFS="for(i=1;i<=NF;i++)if(i==1)printf "%*-s%s",col1,$i,FS;else if(i!=NF)printf "%s%s",$i,FS;else printf "%sn",$i;;' test.txt
Note: if you didn't know what the first column length is you could store the values in an array then finding the max col length along the way and print it all out in the END block.
add a comment
|
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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%2f541215%2fmodify-width-of-first-column-in-file-with-a-variable-number-of-fields-using-awk%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use sprintf
to re-format $1
only.
Ex.
$ awk 'BEGIN" $1 = sprintf("%-3s",$1) 1' file
c1 |c2|c3|c4|c5
c6 |c7|c8|c9|c10
c11|c12|c13|c14|c15
Concise, you can use dynamic formatting with sprintf too: E.g.awk -vf1=3 'BEGIN"$1=sprintf("%-*s",f1,$1)1' test.txt
– A.Danischewski
Sep 11 at 17:36
@A.Danischewski - Well, dang. I've been doing extensive awk programming for ~17 years, and have never come across that one before. To think of all the hassles it would have saved me.
– Paul Sinclair
Sep 12 at 16:47
add a comment
|
You can use sprintf
to re-format $1
only.
Ex.
$ awk 'BEGIN" $1 = sprintf("%-3s",$1) 1' file
c1 |c2|c3|c4|c5
c6 |c7|c8|c9|c10
c11|c12|c13|c14|c15
Concise, you can use dynamic formatting with sprintf too: E.g.awk -vf1=3 'BEGIN"$1=sprintf("%-*s",f1,$1)1' test.txt
– A.Danischewski
Sep 11 at 17:36
@A.Danischewski - Well, dang. I've been doing extensive awk programming for ~17 years, and have never come across that one before. To think of all the hassles it would have saved me.
– Paul Sinclair
Sep 12 at 16:47
add a comment
|
You can use sprintf
to re-format $1
only.
Ex.
$ awk 'BEGIN" $1 = sprintf("%-3s",$1) 1' file
c1 |c2|c3|c4|c5
c6 |c7|c8|c9|c10
c11|c12|c13|c14|c15
You can use sprintf
to re-format $1
only.
Ex.
$ awk 'BEGIN" $1 = sprintf("%-3s",$1) 1' file
c1 |c2|c3|c4|c5
c6 |c7|c8|c9|c10
c11|c12|c13|c14|c15
answered Sep 11 at 16:17
steeldriversteeldriver
45.2k7 gold badges59 silver badges98 bronze badges
45.2k7 gold badges59 silver badges98 bronze badges
Concise, you can use dynamic formatting with sprintf too: E.g.awk -vf1=3 'BEGIN"$1=sprintf("%-*s",f1,$1)1' test.txt
– A.Danischewski
Sep 11 at 17:36
@A.Danischewski - Well, dang. I've been doing extensive awk programming for ~17 years, and have never come across that one before. To think of all the hassles it would have saved me.
– Paul Sinclair
Sep 12 at 16:47
add a comment
|
Concise, you can use dynamic formatting with sprintf too: E.g.awk -vf1=3 'BEGIN"$1=sprintf("%-*s",f1,$1)1' test.txt
– A.Danischewski
Sep 11 at 17:36
@A.Danischewski - Well, dang. I've been doing extensive awk programming for ~17 years, and have never come across that one before. To think of all the hassles it would have saved me.
– Paul Sinclair
Sep 12 at 16:47
Concise, you can use dynamic formatting with sprintf too: E.g.
awk -vf1=3 'BEGIN"$1=sprintf("%-*s",f1,$1)1' test.txt
– A.Danischewski
Sep 11 at 17:36
Concise, you can use dynamic formatting with sprintf too: E.g.
awk -vf1=3 'BEGIN"$1=sprintf("%-*s",f1,$1)1' test.txt
– A.Danischewski
Sep 11 at 17:36
@A.Danischewski - Well, dang. I've been doing extensive awk programming for ~17 years, and have never come across that one before. To think of all the hassles it would have saved me.
– Paul Sinclair
Sep 12 at 16:47
@A.Danischewski - Well, dang. I've been doing extensive awk programming for ~17 years, and have never come across that one before. To think of all the hassles it would have saved me.
– Paul Sinclair
Sep 12 at 16:47
add a comment
|
To figure out the largest/longest length of the first field, and then to reformat the values in the field according that length, you will have to do two separate passes over the file.
awk 'BEGIN "
FNR == NR if (m < (n=length($1))) m = n; next
$1 = sprintf("%-*s", m, $1); print ' file file
(note that the input file is specified twice on the command line)
For the data that you present, this would produce
c1 |c2|c3|c4|c5
c6 |c7|c8|c9|c10
c11|c12|c13|c14|c15
The first pass is handled by the FNR == NR
block, which simply keeps track of the longest field seen so far (m
contains the maximum length seen), and skips to the next line.
The second pass is handled by the last block, which reformats the first field using sprintf()
. The format string %-*s
means "a left-justified string whose width is given by the integer argument before the argument that holds the actual string".
This could obviously be expanded to do all columns by turning the scalar m
into an array that holds the maximum width of each column:
$ awk 'BEGIN "
FNR == NR for (i=1; i<=NF; ++i) if (m[i] < (n=length($i))) m[i] = n; next
for (i=1; i<=NF; ++i) $i = sprintf("%-*s", m[i], $i); print ' file file
c1 |c2 |c3 |c4 |c5
c6 |c7 |c8 |c9 |c10
c11|c12|c13|c14|c15
add a comment
|
To figure out the largest/longest length of the first field, and then to reformat the values in the field according that length, you will have to do two separate passes over the file.
awk 'BEGIN "
FNR == NR if (m < (n=length($1))) m = n; next
$1 = sprintf("%-*s", m, $1); print ' file file
(note that the input file is specified twice on the command line)
For the data that you present, this would produce
c1 |c2|c3|c4|c5
c6 |c7|c8|c9|c10
c11|c12|c13|c14|c15
The first pass is handled by the FNR == NR
block, which simply keeps track of the longest field seen so far (m
contains the maximum length seen), and skips to the next line.
The second pass is handled by the last block, which reformats the first field using sprintf()
. The format string %-*s
means "a left-justified string whose width is given by the integer argument before the argument that holds the actual string".
This could obviously be expanded to do all columns by turning the scalar m
into an array that holds the maximum width of each column:
$ awk 'BEGIN "
FNR == NR for (i=1; i<=NF; ++i) if (m[i] < (n=length($i))) m[i] = n; next
for (i=1; i<=NF; ++i) $i = sprintf("%-*s", m[i], $i); print ' file file
c1 |c2 |c3 |c4 |c5
c6 |c7 |c8 |c9 |c10
c11|c12|c13|c14|c15
add a comment
|
To figure out the largest/longest length of the first field, and then to reformat the values in the field according that length, you will have to do two separate passes over the file.
awk 'BEGIN "
FNR == NR if (m < (n=length($1))) m = n; next
$1 = sprintf("%-*s", m, $1); print ' file file
(note that the input file is specified twice on the command line)
For the data that you present, this would produce
c1 |c2|c3|c4|c5
c6 |c7|c8|c9|c10
c11|c12|c13|c14|c15
The first pass is handled by the FNR == NR
block, which simply keeps track of the longest field seen so far (m
contains the maximum length seen), and skips to the next line.
The second pass is handled by the last block, which reformats the first field using sprintf()
. The format string %-*s
means "a left-justified string whose width is given by the integer argument before the argument that holds the actual string".
This could obviously be expanded to do all columns by turning the scalar m
into an array that holds the maximum width of each column:
$ awk 'BEGIN "
FNR == NR for (i=1; i<=NF; ++i) if (m[i] < (n=length($i))) m[i] = n; next
for (i=1; i<=NF; ++i) $i = sprintf("%-*s", m[i], $i); print ' file file
c1 |c2 |c3 |c4 |c5
c6 |c7 |c8 |c9 |c10
c11|c12|c13|c14|c15
To figure out the largest/longest length of the first field, and then to reformat the values in the field according that length, you will have to do two separate passes over the file.
awk 'BEGIN "
FNR == NR if (m < (n=length($1))) m = n; next
$1 = sprintf("%-*s", m, $1); print ' file file
(note that the input file is specified twice on the command line)
For the data that you present, this would produce
c1 |c2|c3|c4|c5
c6 |c7|c8|c9|c10
c11|c12|c13|c14|c15
The first pass is handled by the FNR == NR
block, which simply keeps track of the longest field seen so far (m
contains the maximum length seen), and skips to the next line.
The second pass is handled by the last block, which reformats the first field using sprintf()
. The format string %-*s
means "a left-justified string whose width is given by the integer argument before the argument that holds the actual string".
This could obviously be expanded to do all columns by turning the scalar m
into an array that holds the maximum width of each column:
$ awk 'BEGIN "
FNR == NR for (i=1; i<=NF; ++i) if (m[i] < (n=length($i))) m[i] = n; next
for (i=1; i<=NF; ++i) $i = sprintf("%-*s", m[i], $i); print ' file file
c1 |c2 |c3 |c4 |c5
c6 |c7 |c8 |c9 |c10
c11|c12|c13|c14|c15
edited Sep 11 at 18:47
answered Sep 11 at 16:31
Kusalananda♦Kusalananda
173k20 gold badges330 silver badges535 bronze badges
173k20 gold badges330 silver badges535 bronze badges
add a comment
|
add a comment
|
The intelligent way is what steeldriver suggested. The needlessly convoluted way is to iterate over every field:
$ awk -F'|' '",$1; for(i=2;i<NF;i++)",$i printf "%sn", $i' file
c1 |c2|c3|c4|c5
c6 |c7|c8|c9|c10
c11|c12|c13|c14|c15
But just sprintf
$1
and be done with it.
1
You've got it a bit backwards, small concise statements generally are more convoluted. Iterating over the fields is less convoluted.
– A.Danischewski
Sep 11 at 19:40
add a comment
|
The intelligent way is what steeldriver suggested. The needlessly convoluted way is to iterate over every field:
$ awk -F'|' '",$1; for(i=2;i<NF;i++)",$i printf "%sn", $i' file
c1 |c2|c3|c4|c5
c6 |c7|c8|c9|c10
c11|c12|c13|c14|c15
But just sprintf
$1
and be done with it.
1
You've got it a bit backwards, small concise statements generally are more convoluted. Iterating over the fields is less convoluted.
– A.Danischewski
Sep 11 at 19:40
add a comment
|
The intelligent way is what steeldriver suggested. The needlessly convoluted way is to iterate over every field:
$ awk -F'|' '",$1; for(i=2;i<NF;i++)",$i printf "%sn", $i' file
c1 |c2|c3|c4|c5
c6 |c7|c8|c9|c10
c11|c12|c13|c14|c15
But just sprintf
$1
and be done with it.
The intelligent way is what steeldriver suggested. The needlessly convoluted way is to iterate over every field:
$ awk -F'|' '",$1; for(i=2;i<NF;i++)",$i printf "%sn", $i' file
c1 |c2|c3|c4|c5
c6 |c7|c8|c9|c10
c11|c12|c13|c14|c15
But just sprintf
$1
and be done with it.
answered Sep 11 at 16:28
terdon♦terdon
154k39 gold badges299 silver badges481 bronze badges
154k39 gold badges299 silver badges481 bronze badges
1
You've got it a bit backwards, small concise statements generally are more convoluted. Iterating over the fields is less convoluted.
– A.Danischewski
Sep 11 at 19:40
add a comment
|
1
You've got it a bit backwards, small concise statements generally are more convoluted. Iterating over the fields is less convoluted.
– A.Danischewski
Sep 11 at 19:40
1
1
You've got it a bit backwards, small concise statements generally are more convoluted. Iterating over the fields is less convoluted.
– A.Danischewski
Sep 11 at 19:40
You've got it a bit backwards, small concise statements generally are more convoluted. Iterating over the fields is less convoluted.
– A.Danischewski
Sep 11 at 19:40
add a comment
|
In Awk you can use a "*" to generate a dynamic printf format string.
If you know the length already you can pass the field length for the first column with -v.
awk -vcol1=3 'BEGINFS="for(i=1;i<=NF;i++)if(i==1)printf "%*-s%s",col1,$i,FS;else if(i!=NF)printf "%s%s",$i,FS;else printf "%sn",$i;;' test.txt
Note: if you didn't know what the first column length is you could store the values in an array then finding the max col length along the way and print it all out in the END block.
add a comment
|
In Awk you can use a "*" to generate a dynamic printf format string.
If you know the length already you can pass the field length for the first column with -v.
awk -vcol1=3 'BEGINFS="for(i=1;i<=NF;i++)if(i==1)printf "%*-s%s",col1,$i,FS;else if(i!=NF)printf "%s%s",$i,FS;else printf "%sn",$i;;' test.txt
Note: if you didn't know what the first column length is you could store the values in an array then finding the max col length along the way and print it all out in the END block.
add a comment
|
In Awk you can use a "*" to generate a dynamic printf format string.
If you know the length already you can pass the field length for the first column with -v.
awk -vcol1=3 'BEGINFS="for(i=1;i<=NF;i++)if(i==1)printf "%*-s%s",col1,$i,FS;else if(i!=NF)printf "%s%s",$i,FS;else printf "%sn",$i;;' test.txt
Note: if you didn't know what the first column length is you could store the values in an array then finding the max col length along the way and print it all out in the END block.
In Awk you can use a "*" to generate a dynamic printf format string.
If you know the length already you can pass the field length for the first column with -v.
awk -vcol1=3 'BEGINFS="for(i=1;i<=NF;i++)if(i==1)printf "%*-s%s",col1,$i,FS;else if(i!=NF)printf "%s%s",$i,FS;else printf "%sn",$i;;' test.txt
Note: if you didn't know what the first column length is you could store the values in an array then finding the max col length along the way and print it all out in the END block.
answered Sep 11 at 17:06
A.DanischewskiA.Danischewski
3422 silver badges7 bronze badges
3422 silver badges7 bronze badges
add a comment
|
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.
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%2f541215%2fmodify-width-of-first-column-in-file-with-a-variable-number-of-fields-using-awk%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
Another way to address it:
sed 's/|/'' '' '' |/;s/(...) */1/'
(here adding extra quotes to insert those 3 spaces as the SE comments squeeze contiguous spaces into one)– Stéphane Chazelas
Sep 11 at 16:23