awk key-value issue for array










0














I meet a awk array issue, details as below:



[~/temp]$ cat test.txt
1
2
3
4
1
2
3


Then I want to count the frequency of the number.



[~/temp]$ awk 'num[$1]++;ENDfor (i in num)"sort -r -n -k1" ' test.txt
1
2
3
2 3
2 2
2 1
1 4


As you see, why does the output of first 3 line '1 2 3' will come blank value?



Thank for your answer.










share|improve this question





















  • Did you Google about how can you do it?
    – tod
    Nov 12 '18 at 2:53















0














I meet a awk array issue, details as below:



[~/temp]$ cat test.txt
1
2
3
4
1
2
3


Then I want to count the frequency of the number.



[~/temp]$ awk 'num[$1]++;ENDfor (i in num)"sort -r -n -k1" ' test.txt
1
2
3
2 3
2 2
2 1
1 4


As you see, why does the output of first 3 line '1 2 3' will come blank value?



Thank for your answer.










share|improve this question





















  • Did you Google about how can you do it?
    – tod
    Nov 12 '18 at 2:53













0












0








0







I meet a awk array issue, details as below:



[~/temp]$ cat test.txt
1
2
3
4
1
2
3


Then I want to count the frequency of the number.



[~/temp]$ awk 'num[$1]++;ENDfor (i in num)"sort -r -n -k1" ' test.txt
1
2
3
2 3
2 2
2 1
1 4


As you see, why does the output of first 3 line '1 2 3' will come blank value?



Thank for your answer.










share|improve this question













I meet a awk array issue, details as below:



[~/temp]$ cat test.txt
1
2
3
4
1
2
3


Then I want to count the frequency of the number.



[~/temp]$ awk 'num[$1]++;ENDfor (i in num)"sort -r -n -k1" ' test.txt
1
2
3
2 3
2 2
2 1
1 4


As you see, why does the output of first 3 line '1 2 3' will come blank value?



Thank for your answer.







linux shell awk






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 2:41









huang cheng

16910




16910











  • Did you Google about how can you do it?
    – tod
    Nov 12 '18 at 2:53
















  • Did you Google about how can you do it?
    – tod
    Nov 12 '18 at 2:53















Did you Google about how can you do it?
– tod
Nov 12 '18 at 2:53




Did you Google about how can you do it?
– tod
Nov 12 '18 at 2:53












2 Answers
2






active

oldest

votes


















1














An awk statement consists of a pattern and related action. Omitted pattern matches every record of input. Omitted action is an alias to print $0, ie. output the current record, which is what you are getting. Looking at the first part of your program:



$ awk 'num[$1]++' file
1
2
3


Let's change that a bit to understand what happens there:



$ awk 'print "NR:",NR,"num["$1"]++:",num[$1]++' file
NR: 1 num[1]++: 0
NR: 2 num[2]++: 0
NR: 3 num[3]++: 0
NR: 4 num[4]++: 0
NR: 5 num[1]++: 1
NR: 6 num[2]++: 1
NR: 7 num[3]++: 1


Since you are using postfix operator num[$1]++ in the pattern, on records 1-4 it gets evaluated to 0 before it's value is incremented. Output would be different if you used the prefix operator ++num[$1] which would first increment the value of the variable after which it would get evaluated and would lead to outputing every record of input, not just the last three, which you were getting.



Correct way would've been to use num[$1]++ as an action, not as a pattern:



$ awk 'num[$1]++' file





share|improve this answer


















  • 1




    really appreciate~
    – huang cheng
    Nov 14 '18 at 6:58


















0














Put your "per line" part in i.e. num[$1]++;



awk programs a a collection of [pattern] actions (the pattern is optional, the is not). Seems that in your case your line is being treated as the pattern.






share|improve this answer






















  • yes, you are right , but how to explain the command when missing , I want to know it more about the significance of in awk.
    – huang cheng
    Nov 12 '18 at 3:01










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',
autoActivateHeartbeat: false,
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%2f53255341%2fawk-key-value-issue-for-array%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









1














An awk statement consists of a pattern and related action. Omitted pattern matches every record of input. Omitted action is an alias to print $0, ie. output the current record, which is what you are getting. Looking at the first part of your program:



$ awk 'num[$1]++' file
1
2
3


Let's change that a bit to understand what happens there:



$ awk 'print "NR:",NR,"num["$1"]++:",num[$1]++' file
NR: 1 num[1]++: 0
NR: 2 num[2]++: 0
NR: 3 num[3]++: 0
NR: 4 num[4]++: 0
NR: 5 num[1]++: 1
NR: 6 num[2]++: 1
NR: 7 num[3]++: 1


Since you are using postfix operator num[$1]++ in the pattern, on records 1-4 it gets evaluated to 0 before it's value is incremented. Output would be different if you used the prefix operator ++num[$1] which would first increment the value of the variable after which it would get evaluated and would lead to outputing every record of input, not just the last three, which you were getting.



Correct way would've been to use num[$1]++ as an action, not as a pattern:



$ awk 'num[$1]++' file





share|improve this answer


















  • 1




    really appreciate~
    – huang cheng
    Nov 14 '18 at 6:58















1














An awk statement consists of a pattern and related action. Omitted pattern matches every record of input. Omitted action is an alias to print $0, ie. output the current record, which is what you are getting. Looking at the first part of your program:



$ awk 'num[$1]++' file
1
2
3


Let's change that a bit to understand what happens there:



$ awk 'print "NR:",NR,"num["$1"]++:",num[$1]++' file
NR: 1 num[1]++: 0
NR: 2 num[2]++: 0
NR: 3 num[3]++: 0
NR: 4 num[4]++: 0
NR: 5 num[1]++: 1
NR: 6 num[2]++: 1
NR: 7 num[3]++: 1


Since you are using postfix operator num[$1]++ in the pattern, on records 1-4 it gets evaluated to 0 before it's value is incremented. Output would be different if you used the prefix operator ++num[$1] which would first increment the value of the variable after which it would get evaluated and would lead to outputing every record of input, not just the last three, which you were getting.



Correct way would've been to use num[$1]++ as an action, not as a pattern:



$ awk 'num[$1]++' file





share|improve this answer


















  • 1




    really appreciate~
    – huang cheng
    Nov 14 '18 at 6:58













1












1








1






An awk statement consists of a pattern and related action. Omitted pattern matches every record of input. Omitted action is an alias to print $0, ie. output the current record, which is what you are getting. Looking at the first part of your program:



$ awk 'num[$1]++' file
1
2
3


Let's change that a bit to understand what happens there:



$ awk 'print "NR:",NR,"num["$1"]++:",num[$1]++' file
NR: 1 num[1]++: 0
NR: 2 num[2]++: 0
NR: 3 num[3]++: 0
NR: 4 num[4]++: 0
NR: 5 num[1]++: 1
NR: 6 num[2]++: 1
NR: 7 num[3]++: 1


Since you are using postfix operator num[$1]++ in the pattern, on records 1-4 it gets evaluated to 0 before it's value is incremented. Output would be different if you used the prefix operator ++num[$1] which would first increment the value of the variable after which it would get evaluated and would lead to outputing every record of input, not just the last three, which you were getting.



Correct way would've been to use num[$1]++ as an action, not as a pattern:



$ awk 'num[$1]++' file





share|improve this answer














An awk statement consists of a pattern and related action. Omitted pattern matches every record of input. Omitted action is an alias to print $0, ie. output the current record, which is what you are getting. Looking at the first part of your program:



$ awk 'num[$1]++' file
1
2
3


Let's change that a bit to understand what happens there:



$ awk 'print "NR:",NR,"num["$1"]++:",num[$1]++' file
NR: 1 num[1]++: 0
NR: 2 num[2]++: 0
NR: 3 num[3]++: 0
NR: 4 num[4]++: 0
NR: 5 num[1]++: 1
NR: 6 num[2]++: 1
NR: 7 num[3]++: 1


Since you are using postfix operator num[$1]++ in the pattern, on records 1-4 it gets evaluated to 0 before it's value is incremented. Output would be different if you used the prefix operator ++num[$1] which would first increment the value of the variable after which it would get evaluated and would lead to outputing every record of input, not just the last three, which you were getting.



Correct way would've been to use num[$1]++ as an action, not as a pattern:



$ awk 'num[$1]++' file






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 '18 at 8:33

























answered Nov 12 '18 at 8:03









James Brown

18.2k31635




18.2k31635







  • 1




    really appreciate~
    – huang cheng
    Nov 14 '18 at 6:58












  • 1




    really appreciate~
    – huang cheng
    Nov 14 '18 at 6:58







1




1




really appreciate~
– huang cheng
Nov 14 '18 at 6:58




really appreciate~
– huang cheng
Nov 14 '18 at 6:58













0














Put your "per line" part in i.e. num[$1]++;



awk programs a a collection of [pattern] actions (the pattern is optional, the is not). Seems that in your case your line is being treated as the pattern.






share|improve this answer






















  • yes, you are right , but how to explain the command when missing , I want to know it more about the significance of in awk.
    – huang cheng
    Nov 12 '18 at 3:01















0














Put your "per line" part in i.e. num[$1]++;



awk programs a a collection of [pattern] actions (the pattern is optional, the is not). Seems that in your case your line is being treated as the pattern.






share|improve this answer






















  • yes, you are right , but how to explain the command when missing , I want to know it more about the significance of in awk.
    – huang cheng
    Nov 12 '18 at 3:01













0












0








0






Put your "per line" part in i.e. num[$1]++;



awk programs a a collection of [pattern] actions (the pattern is optional, the is not). Seems that in your case your line is being treated as the pattern.






share|improve this answer














Put your "per line" part in i.e. num[$1]++;



awk programs a a collection of [pattern] actions (the pattern is optional, the is not). Seems that in your case your line is being treated as the pattern.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 '18 at 3:51

























answered Nov 12 '18 at 2:55









John3136

23.9k33259




23.9k33259











  • yes, you are right , but how to explain the command when missing , I want to know it more about the significance of in awk.
    – huang cheng
    Nov 12 '18 at 3:01
















  • yes, you are right , but how to explain the command when missing , I want to know it more about the significance of in awk.
    – huang cheng
    Nov 12 '18 at 3:01















yes, you are right , but how to explain the command when missing , I want to know it more about the significance of in awk.
– huang cheng
Nov 12 '18 at 3:01




yes, you are right , but how to explain the command when missing , I want to know it more about the significance of in awk.
– huang cheng
Nov 12 '18 at 3:01

















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%2f53255341%2fawk-key-value-issue-for-array%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

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo