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 think
NR > 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?










share|improve this question




















  • 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












  • 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

















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 think
NR > 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?










share|improve this question




















  • 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












  • 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















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 think
NR > 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?










share|improve this question















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 think
NR > 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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 14:47









Lukas Würzburger

2,45331846




2,45331846










asked Nov 10 at 14:26









Sumin Kim

737




737








  • 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












  • 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




    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










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














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





share|improve this answer



















  • 1




    Thank you for your clear answer!
    – Sumin Kim
    Nov 10 at 14:58


















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





share|improve this answer





















  • 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











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%2f53239907%2fwhat-does-this-awk-command-do%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
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





share|improve this answer



















  • 1




    Thank you for your clear answer!
    – Sumin Kim
    Nov 10 at 14:58















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





share|improve this answer



















  • 1




    Thank you for your clear answer!
    – Sumin Kim
    Nov 10 at 14:58













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





share|improve this answer














$ 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






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 14:48

























answered Nov 10 at 14:43









James Brown

17.1k31634




17.1k31634








  • 1




    Thank you for your clear answer!
    – Sumin Kim
    Nov 10 at 14:58














  • 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












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





share|improve this answer





















  • 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















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





share|improve this answer





















  • 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













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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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















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

Coverage of Google Street View

Full-time equivalent

Surfing