counting number of three digit numbers in a text file
I have a text file with hundreds of three digit numbers.
For example:
0 2 3
0 2 3
0 2 9
0 3 9
0 9 2
0 9 2
0 9 9
1 2 2
1 2 2
1 2 2
1 2 9
1 2 9
1 3 3
1 9 2
1 9 2
1 9 2
1 9 3
1 9 9
1 9 9
1 9 9
1 9 9
2 0 2
2 0 3
2 0 9
2 1 2
2 1 2
2 1 3
2 1 9
2 1 9
2 2 4
2 2 5
2 2 5
2 2 5
2 2 6
2 2 6
2 2 8
I want to convert this to show how many of each number is in the file to look something like this:
0 2 3 2
0 9 2 2
1 2 2 3
etc
files batch
add a comment |
I have a text file with hundreds of three digit numbers.
For example:
0 2 3
0 2 3
0 2 9
0 3 9
0 9 2
0 9 2
0 9 9
1 2 2
1 2 2
1 2 2
1 2 9
1 2 9
1 3 3
1 9 2
1 9 2
1 9 2
1 9 3
1 9 9
1 9 9
1 9 9
1 9 9
2 0 2
2 0 3
2 0 9
2 1 2
2 1 2
2 1 3
2 1 9
2 1 9
2 2 4
2 2 5
2 2 5
2 2 5
2 2 6
2 2 6
2 2 8
I want to convert this to show how many of each number is in the file to look something like this:
0 2 3 2
0 9 2 2
1 2 2 3
etc
files batch
the number do not display the right way after I posted the question. Sorry
– Moe Shii
Feb 13 at 1:14
Are those numbers all composed of single digits? If so, you can create a linear array of 1000 and use the numbers as indices to count their occurrences.
– zx485
Feb 13 at 1:37
add a comment |
I have a text file with hundreds of three digit numbers.
For example:
0 2 3
0 2 3
0 2 9
0 3 9
0 9 2
0 9 2
0 9 9
1 2 2
1 2 2
1 2 2
1 2 9
1 2 9
1 3 3
1 9 2
1 9 2
1 9 2
1 9 3
1 9 9
1 9 9
1 9 9
1 9 9
2 0 2
2 0 3
2 0 9
2 1 2
2 1 2
2 1 3
2 1 9
2 1 9
2 2 4
2 2 5
2 2 5
2 2 5
2 2 6
2 2 6
2 2 8
I want to convert this to show how many of each number is in the file to look something like this:
0 2 3 2
0 9 2 2
1 2 2 3
etc
files batch
I have a text file with hundreds of three digit numbers.
For example:
0 2 3
0 2 3
0 2 9
0 3 9
0 9 2
0 9 2
0 9 9
1 2 2
1 2 2
1 2 2
1 2 9
1 2 9
1 3 3
1 9 2
1 9 2
1 9 2
1 9 3
1 9 9
1 9 9
1 9 9
1 9 9
2 0 2
2 0 3
2 0 9
2 1 2
2 1 2
2 1 3
2 1 9
2 1 9
2 2 4
2 2 5
2 2 5
2 2 5
2 2 6
2 2 6
2 2 8
I want to convert this to show how many of each number is in the file to look something like this:
0 2 3 2
0 9 2 2
1 2 2 3
etc
files batch
files batch
edited Feb 13 at 1:58
zx485
1,47131115
1,47131115
asked Feb 13 at 1:12
Moe ShiiMoe Shii
1
1
the number do not display the right way after I posted the question. Sorry
– Moe Shii
Feb 13 at 1:14
Are those numbers all composed of single digits? If so, you can create a linear array of 1000 and use the numbers as indices to count their occurrences.
– zx485
Feb 13 at 1:37
add a comment |
the number do not display the right way after I posted the question. Sorry
– Moe Shii
Feb 13 at 1:14
Are those numbers all composed of single digits? If so, you can create a linear array of 1000 and use the numbers as indices to count their occurrences.
– zx485
Feb 13 at 1:37
the number do not display the right way after I posted the question. Sorry
– Moe Shii
Feb 13 at 1:14
the number do not display the right way after I posted the question. Sorry
– Moe Shii
Feb 13 at 1:14
Are those numbers all composed of single digits? If so, you can create a linear array of 1000 and use the numbers as indices to count their occurrences.
– zx485
Feb 13 at 1:37
Are those numbers all composed of single digits? If so, you can create a linear array of 1000 and use the numbers as indices to count their occurrences.
– zx485
Feb 13 at 1:37
add a comment |
2 Answers
2
active
oldest
votes
sort -n numbers.txt | uniq -c | sed -E 's/^( *[0-9]+) (.*)$/2 1/'
would be the simplest way to achieve your goal. It:
sort
s your numbers first, just in case
counts the length of eachuniq
ue set
sed
s the result to move the count to the rear of each string
add a comment |
The fact that the lines consist of sequences of digits is largely irrelevant - unless you want to do arithmetic on the numbers themselves you can count / uniquify them just like any other strings e.g. using an associative array or hash:
awk '{c[$0]++} END {for (i in c) printf "%st%dn", i, c[i]}' numbers.txt
or
perl -lnE '$c{$_}++ }{ for $k (keys %c) { say "$kt$c{$k}" }' numbers.txt
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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
});
}
});
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%2faskubuntu.com%2fquestions%2f1117804%2fcounting-number-of-three-digit-numbers-in-a-text-file%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
sort -n numbers.txt | uniq -c | sed -E 's/^( *[0-9]+) (.*)$/2 1/'
would be the simplest way to achieve your goal. It:
sort
s your numbers first, just in case
counts the length of eachuniq
ue set
sed
s the result to move the count to the rear of each string
add a comment |
sort -n numbers.txt | uniq -c | sed -E 's/^( *[0-9]+) (.*)$/2 1/'
would be the simplest way to achieve your goal. It:
sort
s your numbers first, just in case
counts the length of eachuniq
ue set
sed
s the result to move the count to the rear of each string
add a comment |
sort -n numbers.txt | uniq -c | sed -E 's/^( *[0-9]+) (.*)$/2 1/'
would be the simplest way to achieve your goal. It:
sort
s your numbers first, just in case
counts the length of eachuniq
ue set
sed
s the result to move the count to the rear of each string
sort -n numbers.txt | uniq -c | sed -E 's/^( *[0-9]+) (.*)$/2 1/'
would be the simplest way to achieve your goal. It:
sort
s your numbers first, just in case
counts the length of eachuniq
ue set
sed
s the result to move the count to the rear of each string
answered Feb 13 at 2:25
AdrianAdrian
1563
1563
add a comment |
add a comment |
The fact that the lines consist of sequences of digits is largely irrelevant - unless you want to do arithmetic on the numbers themselves you can count / uniquify them just like any other strings e.g. using an associative array or hash:
awk '{c[$0]++} END {for (i in c) printf "%st%dn", i, c[i]}' numbers.txt
or
perl -lnE '$c{$_}++ }{ for $k (keys %c) { say "$kt$c{$k}" }' numbers.txt
add a comment |
The fact that the lines consist of sequences of digits is largely irrelevant - unless you want to do arithmetic on the numbers themselves you can count / uniquify them just like any other strings e.g. using an associative array or hash:
awk '{c[$0]++} END {for (i in c) printf "%st%dn", i, c[i]}' numbers.txt
or
perl -lnE '$c{$_}++ }{ for $k (keys %c) { say "$kt$c{$k}" }' numbers.txt
add a comment |
The fact that the lines consist of sequences of digits is largely irrelevant - unless you want to do arithmetic on the numbers themselves you can count / uniquify them just like any other strings e.g. using an associative array or hash:
awk '{c[$0]++} END {for (i in c) printf "%st%dn", i, c[i]}' numbers.txt
or
perl -lnE '$c{$_}++ }{ for $k (keys %c) { say "$kt$c{$k}" }' numbers.txt
The fact that the lines consist of sequences of digits is largely irrelevant - unless you want to do arithmetic on the numbers themselves you can count / uniquify them just like any other strings e.g. using an associative array or hash:
awk '{c[$0]++} END {for (i in c) printf "%st%dn", i, c[i]}' numbers.txt
or
perl -lnE '$c{$_}++ }{ for $k (keys %c) { say "$kt$c{$k}" }' numbers.txt
answered Feb 13 at 2:49
steeldriversteeldriver
68.9k11113184
68.9k11113184
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- 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%2faskubuntu.com%2fquestions%2f1117804%2fcounting-number-of-three-digit-numbers-in-a-text-file%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
the number do not display the right way after I posted the question. Sorry
– Moe Shii
Feb 13 at 1:14
Are those numbers all composed of single digits? If so, you can create a linear array of 1000 and use the numbers as indices to count their occurrences.
– zx485
Feb 13 at 1:37