awk: how do you ensure consistent column numbering when there are blank columns?
up vote
2
down vote
favorite
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
add a comment |
up vote
2
down vote
favorite
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
is it always 1st that could be empty?
– Kent
Nov 9 at 23:12
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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
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
awk
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
add a comment |
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
add a comment |
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
1
That works. Not sure I understand theprintf
though!
– Snowcrash
Nov 11 at 14:19
@Snowcrash, thanks for selecting it right answer. Inprintf
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'sORS
for?
– Snowcrash
Nov 11 at 15:27
@Snowcrash, output record separator, by default its value is new line.
– RavinderSingh13
Nov 11 at 15:55
add a comment |
up vote
1
down vote
with gawk
field widths
$ awk -v FIELDWIDTHS='11 8 3' '{print $2}' file
NAME
adam
bob
That doesn't change anything. I'm usingawk -version awk version 20070501
on the Mac.
– Snowcrash
Nov 11 at 14:18
this is GNUawk
specific, not supported in allawk
s.
– karakfa
Nov 11 at 15:40
add a comment |
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
1
That works. Not sure I understand theprintf
though!
– Snowcrash
Nov 11 at 14:19
@Snowcrash, thanks for selecting it right answer. Inprintf
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'sORS
for?
– Snowcrash
Nov 11 at 15:27
@Snowcrash, output record separator, by default its value is new line.
– RavinderSingh13
Nov 11 at 15:55
add a comment |
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
1
That works. Not sure I understand theprintf
though!
– Snowcrash
Nov 11 at 14:19
@Snowcrash, thanks for selecting it right answer. Inprintf
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'sORS
for?
– Snowcrash
Nov 11 at 15:27
@Snowcrash, output record separator, by default its value is new line.
– RavinderSingh13
Nov 11 at 15:55
add a comment |
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
Could you please try following.
awk '{printf("%s%s",/^ +/?$1:$2,ORS)}' Input_file
Output will be as follows.
NAME
adam
bob
answered Nov 10 at 3:09
RavinderSingh13
24.3k41437
24.3k41437
1
That works. Not sure I understand theprintf
though!
– Snowcrash
Nov 11 at 14:19
@Snowcrash, thanks for selecting it right answer. Inprintf
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'sORS
for?
– Snowcrash
Nov 11 at 15:27
@Snowcrash, output record separator, by default its value is new line.
– RavinderSingh13
Nov 11 at 15:55
add a comment |
1
That works. Not sure I understand theprintf
though!
– Snowcrash
Nov 11 at 14:19
@Snowcrash, thanks for selecting it right answer. Inprintf
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'sORS
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
add a comment |
up vote
1
down vote
with gawk
field widths
$ awk -v FIELDWIDTHS='11 8 3' '{print $2}' file
NAME
adam
bob
That doesn't change anything. I'm usingawk -version awk version 20070501
on the Mac.
– Snowcrash
Nov 11 at 14:18
this is GNUawk
specific, not supported in allawk
s.
– karakfa
Nov 11 at 15:40
add a comment |
up vote
1
down vote
with gawk
field widths
$ awk -v FIELDWIDTHS='11 8 3' '{print $2}' file
NAME
adam
bob
That doesn't change anything. I'm usingawk -version awk version 20070501
on the Mac.
– Snowcrash
Nov 11 at 14:18
this is GNUawk
specific, not supported in allawk
s.
– karakfa
Nov 11 at 15:40
add a comment |
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
with gawk
field widths
$ awk -v FIELDWIDTHS='11 8 3' '{print $2}' file
NAME
adam
bob
answered Nov 10 at 0:10
karakfa
47.3k52738
47.3k52738
That doesn't change anything. I'm usingawk -version awk version 20070501
on the Mac.
– Snowcrash
Nov 11 at 14:18
this is GNUawk
specific, not supported in allawk
s.
– karakfa
Nov 11 at 15:40
add a comment |
That doesn't change anything. I'm usingawk -version awk version 20070501
on the Mac.
– Snowcrash
Nov 11 at 14:18
this is GNUawk
specific, not supported in allawk
s.
– 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 awk
s.– karakfa
Nov 11 at 15:40
this is GNU
awk
specific, not supported in all awk
s.– karakfa
Nov 11 at 15:40
add a comment |
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.
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%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
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
is it always 1st that could be empty?
– Kent
Nov 9 at 23:12