What does this awk command do?
up vote
1
down vote
favorite
What does this awk command do?
awk 'NR > 1 {for(x=1;x<=NF;x++) if(x == 1 || (x >= 4 && x % 2 == 0))
printf "%s", $x (x == NF || x == (NF-1) ? "n":"t")}' depth.txt
> depth_concoct.txt
I thinkNR > 1 means it starts from second line,for(x=1;x<=NF;x++) means for every fields,if(x == 1 || (x >= 4 && x % 2 == 0)) means if x equals 1 or (I don' understand the codes from this part and so on)
and I know that the input file for awk is depth.txt and the output of awk will be saved to depth_concoct.txt.
What does the codes in the middle mean?
awk
add a comment |
up vote
1
down vote
favorite
What does this awk command do?
awk 'NR > 1 {for(x=1;x<=NF;x++) if(x == 1 || (x >= 4 && x % 2 == 0))
printf "%s", $x (x == NF || x == (NF-1) ? "n":"t")}' depth.txt
> depth_concoct.txt
I thinkNR > 1 means it starts from second line,for(x=1;x<=NF;x++) means for every fields,if(x == 1 || (x >= 4 && x % 2 == 0)) means if x equals 1 or (I don' understand the codes from this part and so on)
and I know that the input file for awk is depth.txt and the output of awk will be saved to depth_concoct.txt.
What does the codes in the middle mean?
awk
2
It outputs every 1st, 4th and every even-numbered field after the 4th tab-separated starting from the second record from filedepth.txtand writes it todepth_concoct.txt.
– James Brown
Nov 10 at 14:31
Thank you @James, now I know that if(x == 1 || (x >= 4 && x % 2 == 0)) means that x=1 or x is divisible by 2 from column 4. Can you tell me what $x (x == NF || x == (NF-1) ? "n":"t")} means? Thank you very much!
– Sumin Kim
Nov 10 at 14:33
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
What does this awk command do?
awk 'NR > 1 {for(x=1;x<=NF;x++) if(x == 1 || (x >= 4 && x % 2 == 0))
printf "%s", $x (x == NF || x == (NF-1) ? "n":"t")}' depth.txt
> depth_concoct.txt
I thinkNR > 1 means it starts from second line,for(x=1;x<=NF;x++) means for every fields,if(x == 1 || (x >= 4 && x % 2 == 0)) means if x equals 1 or (I don' understand the codes from this part and so on)
and I know that the input file for awk is depth.txt and the output of awk will be saved to depth_concoct.txt.
What does the codes in the middle mean?
awk
What does this awk command do?
awk 'NR > 1 {for(x=1;x<=NF;x++) if(x == 1 || (x >= 4 && x % 2 == 0))
printf "%s", $x (x == NF || x == (NF-1) ? "n":"t")}' depth.txt
> depth_concoct.txt
I thinkNR > 1 means it starts from second line,for(x=1;x<=NF;x++) means for every fields,if(x == 1 || (x >= 4 && x % 2 == 0)) means if x equals 1 or (I don' understand the codes from this part and so on)
and I know that the input file for awk is depth.txt and the output of awk will be saved to depth_concoct.txt.
What does the codes in the middle mean?
awk
awk
edited Nov 10 at 14:47
Lukas Würzburger
2,44831846
2,44831846
asked Nov 10 at 14:26
Sumin Kim
596
596
2
It outputs every 1st, 4th and every even-numbered field after the 4th tab-separated starting from the second record from filedepth.txtand writes it todepth_concoct.txt.
– James Brown
Nov 10 at 14:31
Thank you @James, now I know that if(x == 1 || (x >= 4 && x % 2 == 0)) means that x=1 or x is divisible by 2 from column 4. Can you tell me what $x (x == NF || x == (NF-1) ? "n":"t")} means? Thank you very much!
– Sumin Kim
Nov 10 at 14:33
add a comment |
2
It outputs every 1st, 4th and every even-numbered field after the 4th tab-separated starting from the second record from filedepth.txtand writes it todepth_concoct.txt.
– James Brown
Nov 10 at 14:31
Thank you @James, now I know that if(x == 1 || (x >= 4 && x % 2 == 0)) means that x=1 or x is divisible by 2 from column 4. Can you tell me what $x (x == NF || x == (NF-1) ? "n":"t")} means? Thank you very much!
– Sumin Kim
Nov 10 at 14:33
2
2
It outputs every 1st, 4th and every even-numbered field after the 4th tab-separated starting from the second record from file
depth.txt and writes it to depth_concoct.txt.– James Brown
Nov 10 at 14:31
It outputs every 1st, 4th and every even-numbered field after the 4th tab-separated starting from the second record from file
depth.txt and writes it to depth_concoct.txt.– James Brown
Nov 10 at 14:31
Thank you @James, now I know that if(x == 1 || (x >= 4 && x % 2 == 0)) means that x=1 or x is divisible by 2 from column 4. Can you tell me what $x (x == NF || x == (NF-1) ? "n":"t")} means? Thank you very much!
– Sumin Kim
Nov 10 at 14:33
Thank you @James, now I know that if(x == 1 || (x >= 4 && x % 2 == 0)) means that x=1 or x is divisible by 2 from column 4. Can you tell me what $x (x == NF || x == (NF-1) ? "n":"t")} means? Thank you very much!
– Sumin Kim
Nov 10 at 14:33
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
$ awk '
NR > 1 { # starting from the second record
for(x=1;x<=NF;x++) # iterate every field
if(x == 1 || (x >= 4 && x % 2 == 0)) # for 1st, 4th and every even-numbered field after 4th
printf "%s", # print the field and after it
$x (x == NF || x == (NF-1) ? "n":"t") # a tab or a newline if its the last field
}' depth.txt > depth_concoct.txt
(x == NF || x == (NF-1) ? "n":"t") is called conditional operator, in this context it's basically streamlined version of:
if( x == NF || x == (NF-1) ) # if this is the last field to be printed
printf "n" # finish the record with a newline
else # else
printf "t"` # print a tab after the field
1
Thank you for your clear answer!
– Sumin Kim
Nov 10 at 14:58
add a comment |
up vote
1
down vote
you can rewrite it as below, which should be trivial to read.
$ awk `NR>1 {printf "%s", $1;
for(x=4;x<=NF;x+=2) printf "t%s", $x;
print ""}' inputfile > outputfile
the complexity of the code is sometimes just an implementation detail.
prints first and every second field starting from the 4th.
Assume your file has 8 fields, this is equivalent to
$ awk -v OFS='t' 'NR>1{print $1,$4,$6,$8}' inputfile > outputfile
Thank you for your example! I also will post my code so it is more readable from now on. Thank you very much!
– Sumin Kim
Nov 10 at 14:59
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
$ awk '
NR > 1 { # starting from the second record
for(x=1;x<=NF;x++) # iterate every field
if(x == 1 || (x >= 4 && x % 2 == 0)) # for 1st, 4th and every even-numbered field after 4th
printf "%s", # print the field and after it
$x (x == NF || x == (NF-1) ? "n":"t") # a tab or a newline if its the last field
}' depth.txt > depth_concoct.txt
(x == NF || x == (NF-1) ? "n":"t") is called conditional operator, in this context it's basically streamlined version of:
if( x == NF || x == (NF-1) ) # if this is the last field to be printed
printf "n" # finish the record with a newline
else # else
printf "t"` # print a tab after the field
1
Thank you for your clear answer!
– Sumin Kim
Nov 10 at 14:58
add a comment |
up vote
1
down vote
accepted
$ awk '
NR > 1 { # starting from the second record
for(x=1;x<=NF;x++) # iterate every field
if(x == 1 || (x >= 4 && x % 2 == 0)) # for 1st, 4th and every even-numbered field after 4th
printf "%s", # print the field and after it
$x (x == NF || x == (NF-1) ? "n":"t") # a tab or a newline if its the last field
}' depth.txt > depth_concoct.txt
(x == NF || x == (NF-1) ? "n":"t") is called conditional operator, in this context it's basically streamlined version of:
if( x == NF || x == (NF-1) ) # if this is the last field to be printed
printf "n" # finish the record with a newline
else # else
printf "t"` # print a tab after the field
1
Thank you for your clear answer!
– Sumin Kim
Nov 10 at 14:58
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
$ awk '
NR > 1 { # starting from the second record
for(x=1;x<=NF;x++) # iterate every field
if(x == 1 || (x >= 4 && x % 2 == 0)) # for 1st, 4th and every even-numbered field after 4th
printf "%s", # print the field and after it
$x (x == NF || x == (NF-1) ? "n":"t") # a tab or a newline if its the last field
}' depth.txt > depth_concoct.txt
(x == NF || x == (NF-1) ? "n":"t") is called conditional operator, in this context it's basically streamlined version of:
if( x == NF || x == (NF-1) ) # if this is the last field to be printed
printf "n" # finish the record with a newline
else # else
printf "t"` # print a tab after the field
$ awk '
NR > 1 { # starting from the second record
for(x=1;x<=NF;x++) # iterate every field
if(x == 1 || (x >= 4 && x % 2 == 0)) # for 1st, 4th and every even-numbered field after 4th
printf "%s", # print the field and after it
$x (x == NF || x == (NF-1) ? "n":"t") # a tab or a newline if its the last field
}' depth.txt > depth_concoct.txt
(x == NF || x == (NF-1) ? "n":"t") is called conditional operator, in this context it's basically streamlined version of:
if( x == NF || x == (NF-1) ) # if this is the last field to be printed
printf "n" # finish the record with a newline
else # else
printf "t"` # print a tab after the field
edited Nov 10 at 14:48
answered Nov 10 at 14:43
James Brown
17k31634
17k31634
1
Thank you for your clear answer!
– Sumin Kim
Nov 10 at 14:58
add a comment |
1
Thank you for your clear answer!
– Sumin Kim
Nov 10 at 14:58
1
1
Thank you for your clear answer!
– Sumin Kim
Nov 10 at 14:58
Thank you for your clear answer!
– Sumin Kim
Nov 10 at 14:58
add a comment |
up vote
1
down vote
you can rewrite it as below, which should be trivial to read.
$ awk `NR>1 {printf "%s", $1;
for(x=4;x<=NF;x+=2) printf "t%s", $x;
print ""}' inputfile > outputfile
the complexity of the code is sometimes just an implementation detail.
prints first and every second field starting from the 4th.
Assume your file has 8 fields, this is equivalent to
$ awk -v OFS='t' 'NR>1{print $1,$4,$6,$8}' inputfile > outputfile
Thank you for your example! I also will post my code so it is more readable from now on. Thank you very much!
– Sumin Kim
Nov 10 at 14:59
add a comment |
up vote
1
down vote
you can rewrite it as below, which should be trivial to read.
$ awk `NR>1 {printf "%s", $1;
for(x=4;x<=NF;x+=2) printf "t%s", $x;
print ""}' inputfile > outputfile
the complexity of the code is sometimes just an implementation detail.
prints first and every second field starting from the 4th.
Assume your file has 8 fields, this is equivalent to
$ awk -v OFS='t' 'NR>1{print $1,$4,$6,$8}' inputfile > outputfile
Thank you for your example! I also will post my code so it is more readable from now on. Thank you very much!
– Sumin Kim
Nov 10 at 14:59
add a comment |
up vote
1
down vote
up vote
1
down vote
you can rewrite it as below, which should be trivial to read.
$ awk `NR>1 {printf "%s", $1;
for(x=4;x<=NF;x+=2) printf "t%s", $x;
print ""}' inputfile > outputfile
the complexity of the code is sometimes just an implementation detail.
prints first and every second field starting from the 4th.
Assume your file has 8 fields, this is equivalent to
$ awk -v OFS='t' 'NR>1{print $1,$4,$6,$8}' inputfile > outputfile
you can rewrite it as below, which should be trivial to read.
$ awk `NR>1 {printf "%s", $1;
for(x=4;x<=NF;x+=2) printf "t%s", $x;
print ""}' inputfile > outputfile
the complexity of the code is sometimes just an implementation detail.
prints first and every second field starting from the 4th.
Assume your file has 8 fields, this is equivalent to
$ awk -v OFS='t' 'NR>1{print $1,$4,$6,$8}' inputfile > outputfile
answered Nov 10 at 14:44
karakfa
46.6k52538
46.6k52538
Thank you for your example! I also will post my code so it is more readable from now on. Thank you very much!
– Sumin Kim
Nov 10 at 14:59
add a comment |
Thank you for your example! I also will post my code so it is more readable from now on. Thank you very much!
– Sumin Kim
Nov 10 at 14:59
Thank you for your example! I also will post my code so it is more readable from now on. Thank you very much!
– Sumin Kim
Nov 10 at 14:59
Thank you for your example! I also will post my code so it is more readable from now on. Thank you very much!
– Sumin Kim
Nov 10 at 14:59
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%2fstackoverflow.com%2fquestions%2f53239907%2fwhat-does-this-awk-command-do%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
2
It outputs every 1st, 4th and every even-numbered field after the 4th tab-separated starting from the second record from file
depth.txtand writes it todepth_concoct.txt.– James Brown
Nov 10 at 14:31
Thank you @James, now I know that if(x == 1 || (x >= 4 && x % 2 == 0)) means that x=1 or x is divisible by 2 from column 4. Can you tell me what $x (x == NF || x == (NF-1) ? "n":"t")} means? Thank you very much!
– Sumin Kim
Nov 10 at 14:33