awk: how do you ensure consistent column numbering when there are blank columns?











up vote
2
down vote

favorite
1












my_file is like this:



SELECTED   NAME    AGE
* adam 30
bob 70


I'd like to output:



adam
bob


however, if I try: cat my_file|awk '{print $2}' it outputs



NAME
adam
70


Any suggestions on how you get awk to account for a blank column?










share|improve this question






















  • is it always 1st that could be empty?
    – Kent
    Nov 9 at 23:12















up vote
2
down vote

favorite
1












my_file is like this:



SELECTED   NAME    AGE
* adam 30
bob 70


I'd like to output:



adam
bob


however, if I try: cat my_file|awk '{print $2}' it outputs



NAME
adam
70


Any suggestions on how you get awk to account for a blank column?










share|improve this question






















  • is it always 1st that could be empty?
    – Kent
    Nov 9 at 23:12













up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





my_file is like this:



SELECTED   NAME    AGE
* adam 30
bob 70


I'd like to output:



adam
bob


however, if I try: cat my_file|awk '{print $2}' it outputs



NAME
adam
70


Any suggestions on how you get awk to account for a blank column?










share|improve this question













my_file is like this:



SELECTED   NAME    AGE
* adam 30
bob 70


I'd like to output:



adam
bob


however, if I try: cat my_file|awk '{print $2}' it outputs



NAME
adam
70


Any suggestions on how you get awk to account for a blank column?







awk






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 22:46









Snowcrash

36.6k39131209




36.6k39131209












  • is it always 1st that could be empty?
    – Kent
    Nov 9 at 23:12


















  • is it always 1st that could be empty?
    – Kent
    Nov 9 at 23:12
















is it always 1st that could be empty?
– Kent
Nov 9 at 23:12




is it always 1st that could be empty?
– Kent
Nov 9 at 23:12












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










Could you please try following.



awk '{printf("%s%s",/^ +/?$1:$2,ORS)}'  Input_file


Output will be as follows.



NAME
adam
bob





share|improve this answer

















  • 1




    That works. Not sure I understand the printf though!
    – Snowcrash
    Nov 11 at 14:19










  • @Snowcrash, thanks for selecting it right answer. In printf you could mention the type of variable which you are printing in this case I have mentoined %s which means to print string(s) and checking condition here /^ +/ means if a line starts with space then ? which tells condition is TRUE prints $1 else : will print $2, let me know if this is clear now?
    – RavinderSingh13
    Nov 11 at 14:27










  • What's ORS for?
    – Snowcrash
    Nov 11 at 15:27










  • @Snowcrash, output record separator, by default its value is new line.
    – RavinderSingh13
    Nov 11 at 15:55


















up vote
1
down vote













with gawk field widths



$ awk -v FIELDWIDTHS='11 8 3' '{print $2}' file

NAME
adam
bob





share|improve this answer





















  • That doesn't change anything. I'm using awk -version awk version 20070501 on the Mac.
    – Snowcrash
    Nov 11 at 14:18










  • this is GNU awk specific, not supported in all awks.
    – karakfa
    Nov 11 at 15:40











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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/3.0/"u003ecc by-sa 3.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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234232%2fawk-how-do-you-ensure-consistent-column-numbering-when-there-are-blank-columns%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Could you please try following.



awk '{printf("%s%s",/^ +/?$1:$2,ORS)}'  Input_file


Output will be as follows.



NAME
adam
bob





share|improve this answer

















  • 1




    That works. Not sure I understand the printf though!
    – Snowcrash
    Nov 11 at 14:19










  • @Snowcrash, thanks for selecting it right answer. In printf you could mention the type of variable which you are printing in this case I have mentoined %s which means to print string(s) and checking condition here /^ +/ means if a line starts with space then ? which tells condition is TRUE prints $1 else : will print $2, let me know if this is clear now?
    – RavinderSingh13
    Nov 11 at 14:27










  • What's ORS for?
    – Snowcrash
    Nov 11 at 15:27










  • @Snowcrash, output record separator, by default its value is new line.
    – RavinderSingh13
    Nov 11 at 15:55















up vote
0
down vote



accepted










Could you please try following.



awk '{printf("%s%s",/^ +/?$1:$2,ORS)}'  Input_file


Output will be as follows.



NAME
adam
bob





share|improve this answer

















  • 1




    That works. Not sure I understand the printf though!
    – Snowcrash
    Nov 11 at 14:19










  • @Snowcrash, thanks for selecting it right answer. In printf you could mention the type of variable which you are printing in this case I have mentoined %s which means to print string(s) and checking condition here /^ +/ means if a line starts with space then ? which tells condition is TRUE prints $1 else : will print $2, let me know if this is clear now?
    – RavinderSingh13
    Nov 11 at 14:27










  • What's ORS for?
    – Snowcrash
    Nov 11 at 15:27










  • @Snowcrash, output record separator, by default its value is new line.
    – RavinderSingh13
    Nov 11 at 15:55













up vote
0
down vote



accepted







up vote
0
down vote



accepted






Could you please try following.



awk '{printf("%s%s",/^ +/?$1:$2,ORS)}'  Input_file


Output will be as follows.



NAME
adam
bob





share|improve this answer












Could you please try following.



awk '{printf("%s%s",/^ +/?$1:$2,ORS)}'  Input_file


Output will be as follows.



NAME
adam
bob






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 3:09









RavinderSingh13

24.3k41437




24.3k41437








  • 1




    That works. Not sure I understand the printf though!
    – Snowcrash
    Nov 11 at 14:19










  • @Snowcrash, thanks for selecting it right answer. In printf you could mention the type of variable which you are printing in this case I have mentoined %s which means to print string(s) and checking condition here /^ +/ means if a line starts with space then ? which tells condition is TRUE prints $1 else : will print $2, let me know if this is clear now?
    – RavinderSingh13
    Nov 11 at 14:27










  • What's ORS for?
    – Snowcrash
    Nov 11 at 15:27










  • @Snowcrash, output record separator, by default its value is new line.
    – RavinderSingh13
    Nov 11 at 15:55














  • 1




    That works. Not sure I understand the printf though!
    – Snowcrash
    Nov 11 at 14:19










  • @Snowcrash, thanks for selecting it right answer. In printf you could mention the type of variable which you are printing in this case I have mentoined %s which means to print string(s) and checking condition here /^ +/ means if a line starts with space then ? which tells condition is TRUE prints $1 else : will print $2, let me know if this is clear now?
    – RavinderSingh13
    Nov 11 at 14:27










  • What's ORS for?
    – Snowcrash
    Nov 11 at 15:27










  • @Snowcrash, output record separator, by default its value is new line.
    – RavinderSingh13
    Nov 11 at 15:55








1




1




That works. Not sure I understand the printf though!
– Snowcrash
Nov 11 at 14:19




That works. Not sure I understand the printf though!
– Snowcrash
Nov 11 at 14:19












@Snowcrash, thanks for selecting it right answer. In printf you could mention the type of variable which you are printing in this case I have mentoined %s which means to print string(s) and checking condition here /^ +/ means if a line starts with space then ? which tells condition is TRUE prints $1 else : will print $2, let me know if this is clear now?
– RavinderSingh13
Nov 11 at 14:27




@Snowcrash, thanks for selecting it right answer. In printf you could mention the type of variable which you are printing in this case I have mentoined %s which means to print string(s) and checking condition here /^ +/ means if a line starts with space then ? which tells condition is TRUE prints $1 else : will print $2, let me know if this is clear now?
– RavinderSingh13
Nov 11 at 14:27












What's ORS for?
– Snowcrash
Nov 11 at 15:27




What's ORS for?
– Snowcrash
Nov 11 at 15:27












@Snowcrash, output record separator, by default its value is new line.
– RavinderSingh13
Nov 11 at 15:55




@Snowcrash, output record separator, by default its value is new line.
– RavinderSingh13
Nov 11 at 15:55












up vote
1
down vote













with gawk field widths



$ awk -v FIELDWIDTHS='11 8 3' '{print $2}' file

NAME
adam
bob





share|improve this answer





















  • That doesn't change anything. I'm using awk -version awk version 20070501 on the Mac.
    – Snowcrash
    Nov 11 at 14:18










  • this is GNU awk specific, not supported in all awks.
    – karakfa
    Nov 11 at 15:40















up vote
1
down vote













with gawk field widths



$ awk -v FIELDWIDTHS='11 8 3' '{print $2}' file

NAME
adam
bob





share|improve this answer





















  • That doesn't change anything. I'm using awk -version awk version 20070501 on the Mac.
    – Snowcrash
    Nov 11 at 14:18










  • this is GNU awk specific, not supported in all awks.
    – karakfa
    Nov 11 at 15:40













up vote
1
down vote










up vote
1
down vote









with gawk field widths



$ awk -v FIELDWIDTHS='11 8 3' '{print $2}' file

NAME
adam
bob





share|improve this answer












with gawk field widths



$ awk -v FIELDWIDTHS='11 8 3' '{print $2}' file

NAME
adam
bob






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 0:10









karakfa

47.3k52738




47.3k52738












  • That doesn't change anything. I'm using awk -version awk version 20070501 on the Mac.
    – Snowcrash
    Nov 11 at 14:18










  • this is GNU awk specific, not supported in all awks.
    – karakfa
    Nov 11 at 15:40


















  • That doesn't change anything. I'm using awk -version awk version 20070501 on the Mac.
    – Snowcrash
    Nov 11 at 14:18










  • this is GNU awk specific, not supported in all awks.
    – karakfa
    Nov 11 at 15:40
















That doesn't change anything. I'm using awk -version awk version 20070501 on the Mac.
– Snowcrash
Nov 11 at 14:18




That doesn't change anything. I'm using awk -version awk version 20070501 on the Mac.
– Snowcrash
Nov 11 at 14:18












this is GNU awk specific, not supported in all awks.
– karakfa
Nov 11 at 15:40




this is GNU awk specific, not supported in all awks.
– karakfa
Nov 11 at 15:40


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234232%2fawk-how-do-you-ensure-consistent-column-numbering-when-there-are-blank-columns%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff