Modify width of first column in file with a variable number of fields, using awkAwk: expanding first field along the columnHow to add column in the beginning of file using perl?unix: replace one entire column in one file with a single value from another fileJoining two files matching two columns with mismatches and in each matching line, substitute second column from file 1 into 6th column in file 2How to print all fields containing one of two strings in a table with awkHow to compare 2 files with common columns and then get the output file with columns from each fileHow to use awk to correct and unify a corrupted file with multiple columns and lines?awk - , fixed width columnsAwk for merging multiple files with common columnClean formatting of output within bash scripts
Uncovering the Accelerated Dragon opening
Is a suit against a Univeristy Dorm for changing policies on a whim likely to succeed (USA)?
Why is the T-1000 humanoid?
In Germany, how can I maximize the impact of my charitable donations?
Where can I get an anonymous Rav Kav card issued?
Do all humans have an identical nucleotide sequence for certain proteins, e.g haemoglobin?
How is Team Scooby Doo (Mystery Inc.) funded?
Mean π: Archimedes vs. Gauss - π computation through generalized means
How are aircraft depainted?
Is there a reliable way to hide/convey a message in vocal expressions (speech, song,...)
How can I fix a framing mistake so I can drywall?
Does my opponent need to prove his creature has morph?
How do email clients "send later" without storing a password?
What exactly is a marshrutka (маршрутка)?
I was promised a work PC but still awaiting approval 3 months later so using my own laptop - Is it fair to ask employer for laptop insurance?
Can I disable a battery powered device by reversing half of its batteries?
How to work with a technician hired with a grant who argues everything
Why does Coq include let-expressions in its core language
What is my breathable atmosphere composed of?
Do ibuprofen or paracetamol cause hearing loss?
Random point on a sphere
Integer Decision Variables Always Forced to Zero in Minimization Problem (MINLP)
How do I determine what is "magic" and "bearing magic" for Detect Magic?
A shy person in a queue
Modify width of first column in file with a variable number of fields, using awk
Awk: expanding first field along the columnHow to add column in the beginning of file using perl?unix: replace one entire column in one file with a single value from another fileJoining two files matching two columns with mismatches and in each matching line, substitute second column from file 1 into 6th column in file 2How to print all fields containing one of two strings in a table with awkHow to compare 2 files with common columns and then get the output file with columns from each fileHow to use awk to correct and unify a corrupted file with multiple columns and lines?awk - , fixed width columnsAwk for merging multiple files with common columnClean formatting of output within bash scripts
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I've seen a lot of questions regarding formatting in awk. I understand how to use printf, but I dont 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 every record's first field is the width of c11 of longest cell contained 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
Lets 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 files text-formatting printf
New contributor
Kayli O'Keefe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment
|
I've seen a lot of questions regarding formatting in awk. I understand how to use printf, but I dont 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 every record's first field is the width of c11 of longest cell contained 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
Lets 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 files text-formatting printf
New contributor
Kayli O'Keefe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
7 hours ago
add a comment
|
I've seen a lot of questions regarding formatting in awk. I understand how to use printf, but I dont 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 every record's first field is the width of c11 of longest cell contained 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
Lets 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 files text-formatting printf
New contributor
Kayli O'Keefe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I've seen a lot of questions regarding formatting in awk. I understand how to use printf, but I dont 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 every record's first field is the width of c11 of longest cell contained 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
Lets 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 files text-formatting printf
awk files text-formatting printf
New contributor
Kayli O'Keefe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Kayli O'Keefe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 5 hours ago
Kusalananda♦
163k19 gold badges321 silver badges508 bronze badges
163k19 gold badges321 silver badges508 bronze badges
New contributor
Kayli O'Keefe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 8 hours ago
Kayli O'KeefeKayli O'Keefe
233 bronze badges
233 bronze badges
New contributor
Kayli O'Keefe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Kayli O'Keefe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
7 hours ago
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
7 hours ago
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
7 hours ago
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
7 hours ago
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
6 hours ago
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
1
@EdMorton Yes, neat, thanks.
– Kusalananda♦
5 hours ago
add a comment
|
The intelligent way is what steeldriver suggested. The needlessly convoluted way is to iterate over every field:
$ awk -F'|' 'printf "%-3s' 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.
You've got it a bit backwards, small concise statements generally are more convoluted. Iterating over the fields is less convoluted.
– A.Danischewski
4 hours ago
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
);
);
Kayli O'Keefe is a new contributor. Be nice, and check out our Code of Conduct.
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
6 hours ago
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
6 hours ago
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 8 hours ago
steeldriversteeldriver
43.2k5 gold badges56 silver badges95 bronze badges
43.2k5 gold badges56 silver badges95 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
6 hours ago
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
6 hours ago
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
6 hours ago
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
6 hours ago
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
1
@EdMorton Yes, neat, thanks.
– Kusalananda♦
5 hours ago
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
1
@EdMorton Yes, neat, thanks.
– Kusalananda♦
5 hours ago
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 5 hours ago
answered 7 hours ago
Kusalananda♦Kusalananda
163k19 gold badges321 silver badges508 bronze badges
163k19 gold badges321 silver badges508 bronze badges
1
@EdMorton Yes, neat, thanks.
– Kusalananda♦
5 hours ago
add a comment
|
1
@EdMorton Yes, neat, thanks.
– Kusalananda♦
5 hours ago
1
1
@EdMorton Yes, neat, thanks.
– Kusalananda♦
5 hours ago
@EdMorton Yes, neat, thanks.
– Kusalananda♦
5 hours ago
add a comment
|
The intelligent way is what steeldriver suggested. The needlessly convoluted way is to iterate over every field:
$ awk -F'|' 'printf "%-3s' 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.
You've got it a bit backwards, small concise statements generally are more convoluted. Iterating over the fields is less convoluted.
– A.Danischewski
4 hours ago
add a comment
|
The intelligent way is what steeldriver suggested. The needlessly convoluted way is to iterate over every field:
$ awk -F'|' 'printf "%-3s' 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.
You've got it a bit backwards, small concise statements generally are more convoluted. Iterating over the fields is less convoluted.
– A.Danischewski
4 hours ago
add a comment
|
The intelligent way is what steeldriver suggested. The needlessly convoluted way is to iterate over every field:
$ awk -F'|' 'printf "%-3s' 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'|' 'printf "%-3s' 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 7 hours ago
terdon♦terdon
142k35 gold badges294 silver badges471 bronze badges
142k35 gold badges294 silver badges471 bronze badges
You've got it a bit backwards, small concise statements generally are more convoluted. Iterating over the fields is less convoluted.
– A.Danischewski
4 hours ago
add a comment
|
You've got it a bit backwards, small concise statements generally are more convoluted. Iterating over the fields is less convoluted.
– A.Danischewski
4 hours ago
You've got it a bit backwards, small concise statements generally are more convoluted. Iterating over the fields is less convoluted.
– A.Danischewski
4 hours ago
You've got it a bit backwards, small concise statements generally are more convoluted. Iterating over the fields is less convoluted.
– A.Danischewski
4 hours ago
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 7 hours ago
A.DanischewskiA.Danischewski
2642 silver badges7 bronze badges
2642 silver badges7 bronze badges
add a comment
|
add a comment
|
Kayli O'Keefe is a new contributor. Be nice, and check out our Code of Conduct.
Kayli O'Keefe is a new contributor. Be nice, and check out our Code of Conduct.
Kayli O'Keefe is a new contributor. Be nice, and check out our Code of Conduct.
Kayli O'Keefe is a new contributor. Be nice, and check out our Code of Conduct.
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
7 hours ago