Count values of dict-like object in multiple filesWhat is the best way to find a list of several strings within a large text fileCount occurrences of line in multiple filesHelp with using quotes in grep searchesHow to grep between words in a log?Combining strings command and grep: how to limit results to null-terminated stringsExtract all lines from a word to another with my match in betweenHow can I find which csv files are related (i.e., have foreign key references) to others?compare two files and print matches - large filesgrep can't get the word I need, but Linux continues to do the next job. How do I stop it?Find all occurrences of string after another string in all files
How dangerous is XSS
How to show a landlord what we have in savings?
Unlock My Phone! February 2018
What are the G forces leaving Earth orbit?
Does marriage to a non-Numenorean disqualify a candidate for the crown of Gondor?
Mathematica command that allows it to read my intentions
What reasons are there for a Capitalist to oppose a 100% inheritance tax?
how do we prove that a sum of two periods is still a period?
How obscure is the use of 令 in 令和?
What is an equivalently powerful replacement spell for Yuan-Ti's Suggestion spell?
How exploitable/balanced is this homebrew spell: Spell Permanency?
How to compactly explain secondary and tertiary characters without resorting to stereotypes?
How can a day be of 24 hours?
If a warlock makes a Dancing Sword their pact weapon, is there a way to prevent it from disappearing if it's farther away for more than a minute?
Getting extremely large arrows with tikzcd
Am I breaking OOP practice with this architecture?
How can I prove that a state of equilibrium is unstable?
Does the Idaho Potato Commission associate potato skins with healthy eating?
What exactly is ineptocracy?
Why was Sir Cadogan fired?
Placement of More Information/Help Icon button for Radio Buttons
Finitely generated matrix groups whose eigenvalues are all algebraic
Avoiding the "not like other girls" trope?
Are British MPs missing the point, with these 'Indicative Votes'?
Count values of dict-like object in multiple files
What is the best way to find a list of several strings within a large text fileCount occurrences of line in multiple filesHelp with using quotes in grep searchesHow to grep between words in a log?Combining strings command and grep: how to limit results to null-terminated stringsExtract all lines from a word to another with my match in betweenHow can I find which csv files are related (i.e., have foreign key references) to others?compare two files and print matches - large filesgrep can't get the word I need, but Linux continues to do the next job. How do I stop it?Find all occurrences of string after another string in all files
I have three txt files that have this format:
File 1:
"username1":10,
"username2":11,
"username3":100,
File 2:
"username1":4,
"username5":15,
"username6":25,
File 3:
"username1":6,
"username2":19,
"username6":25,
I would like a json file (a dict
object) containing the sum of the values of each username. That is, I would like:
"username1":20,
"username2":30,
"username3":100,
"username5":15,
"username6":50,
Each username could appear in each file; each username will occur a maximum of 1 time in each file. The string usernamei
is an alphanumeric string of length >=1. It could be Stacy60
or evergreen
or 0
, etc.
That is, if username1
appears in file1, it only appears once in file1; it could appear again in file2 or file3 or both, but only once in each file.
Since these are txt files, I'm having a hard time reading them in as dict objects in Python, and thought there might be a nice grep
or cat
solution to this. I'm not sure how to get it right, though. I've tried:
grep -o '[[:alnum:]]":"[0-9]+" ./file_*.txt | cut -d '"' f1 | sort | uniq -c > all_usernames.json
It didn't work, and I'm not sure how to fix it.
grep
|
show 7 more comments
I have three txt files that have this format:
File 1:
"username1":10,
"username2":11,
"username3":100,
File 2:
"username1":4,
"username5":15,
"username6":25,
File 3:
"username1":6,
"username2":19,
"username6":25,
I would like a json file (a dict
object) containing the sum of the values of each username. That is, I would like:
"username1":20,
"username2":30,
"username3":100,
"username5":15,
"username6":50,
Each username could appear in each file; each username will occur a maximum of 1 time in each file. The string usernamei
is an alphanumeric string of length >=1. It could be Stacy60
or evergreen
or 0
, etc.
That is, if username1
appears in file1, it only appears once in file1; it could appear again in file2 or file3 or both, but only once in each file.
Since these are txt files, I'm having a hard time reading them in as dict objects in Python, and thought there might be a nice grep
or cat
solution to this. I'm not sure how to get it right, though. I've tried:
grep -o '[[:alnum:]]":"[0-9]+" ./file_*.txt | cut -d '"' f1 | sort | uniq -c > all_usernames.json
It didn't work, and I'm not sure how to fix it.
grep
1
Does the last entry in each object actually have a comma at the end? If so, neither input nor output is valid JSON.
– Kusalananda♦
2 days ago
Yes, each entry does have a comma at the end.
– StatsSorceress
2 days ago
1
Python has a good json package that would make short work of this.
– stolenmoment
2 days ago
Except each input file is not in json format; they're txt files. Does that matter?
– StatsSorceress
2 days ago
@StatsSorceress There can be various nice solutions through command line, but are you sure you wouldn't like a python solution ? that format is exactly python's format for dict literals and those files can be easily slurped in by a very short and simple python script
– LL3
2 days ago
|
show 7 more comments
I have three txt files that have this format:
File 1:
"username1":10,
"username2":11,
"username3":100,
File 2:
"username1":4,
"username5":15,
"username6":25,
File 3:
"username1":6,
"username2":19,
"username6":25,
I would like a json file (a dict
object) containing the sum of the values of each username. That is, I would like:
"username1":20,
"username2":30,
"username3":100,
"username5":15,
"username6":50,
Each username could appear in each file; each username will occur a maximum of 1 time in each file. The string usernamei
is an alphanumeric string of length >=1. It could be Stacy60
or evergreen
or 0
, etc.
That is, if username1
appears in file1, it only appears once in file1; it could appear again in file2 or file3 or both, but only once in each file.
Since these are txt files, I'm having a hard time reading them in as dict objects in Python, and thought there might be a nice grep
or cat
solution to this. I'm not sure how to get it right, though. I've tried:
grep -o '[[:alnum:]]":"[0-9]+" ./file_*.txt | cut -d '"' f1 | sort | uniq -c > all_usernames.json
It didn't work, and I'm not sure how to fix it.
grep
I have three txt files that have this format:
File 1:
"username1":10,
"username2":11,
"username3":100,
File 2:
"username1":4,
"username5":15,
"username6":25,
File 3:
"username1":6,
"username2":19,
"username6":25,
I would like a json file (a dict
object) containing the sum of the values of each username. That is, I would like:
"username1":20,
"username2":30,
"username3":100,
"username5":15,
"username6":50,
Each username could appear in each file; each username will occur a maximum of 1 time in each file. The string usernamei
is an alphanumeric string of length >=1. It could be Stacy60
or evergreen
or 0
, etc.
That is, if username1
appears in file1, it only appears once in file1; it could appear again in file2 or file3 or both, but only once in each file.
Since these are txt files, I'm having a hard time reading them in as dict objects in Python, and thought there might be a nice grep
or cat
solution to this. I'm not sure how to get it right, though. I've tried:
grep -o '[[:alnum:]]":"[0-9]+" ./file_*.txt | cut -d '"' f1 | sort | uniq -c > all_usernames.json
It didn't work, and I'm not sure how to fix it.
grep
grep
asked 2 days ago
StatsSorceressStatsSorceress
16517
16517
1
Does the last entry in each object actually have a comma at the end? If so, neither input nor output is valid JSON.
– Kusalananda♦
2 days ago
Yes, each entry does have a comma at the end.
– StatsSorceress
2 days ago
1
Python has a good json package that would make short work of this.
– stolenmoment
2 days ago
Except each input file is not in json format; they're txt files. Does that matter?
– StatsSorceress
2 days ago
@StatsSorceress There can be various nice solutions through command line, but are you sure you wouldn't like a python solution ? that format is exactly python's format for dict literals and those files can be easily slurped in by a very short and simple python script
– LL3
2 days ago
|
show 7 more comments
1
Does the last entry in each object actually have a comma at the end? If so, neither input nor output is valid JSON.
– Kusalananda♦
2 days ago
Yes, each entry does have a comma at the end.
– StatsSorceress
2 days ago
1
Python has a good json package that would make short work of this.
– stolenmoment
2 days ago
Except each input file is not in json format; they're txt files. Does that matter?
– StatsSorceress
2 days ago
@StatsSorceress There can be various nice solutions through command line, but are you sure you wouldn't like a python solution ? that format is exactly python's format for dict literals and those files can be easily slurped in by a very short and simple python script
– LL3
2 days ago
1
1
Does the last entry in each object actually have a comma at the end? If so, neither input nor output is valid JSON.
– Kusalananda♦
2 days ago
Does the last entry in each object actually have a comma at the end? If so, neither input nor output is valid JSON.
– Kusalananda♦
2 days ago
Yes, each entry does have a comma at the end.
– StatsSorceress
2 days ago
Yes, each entry does have a comma at the end.
– StatsSorceress
2 days ago
1
1
Python has a good json package that would make short work of this.
– stolenmoment
2 days ago
Python has a good json package that would make short work of this.
– stolenmoment
2 days ago
Except each input file is not in json format; they're txt files. Does that matter?
– StatsSorceress
2 days ago
Except each input file is not in json format; they're txt files. Does that matter?
– StatsSorceress
2 days ago
@StatsSorceress There can be various nice solutions through command line, but are you sure you wouldn't like a python solution ? that format is exactly python's format for dict literals and those files can be easily slurped in by a very short and simple python script
– LL3
2 days ago
@StatsSorceress There can be various nice solutions through command line, but are you sure you wouldn't like a python solution ? that format is exactly python's format for dict literals and those files can be easily slurped in by a very short and simple python script
– LL3
2 days ago
|
show 7 more comments
0
active
oldest
votes
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f509758%2fcount-values-of-dict-like-object-in-multiple-files%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f509758%2fcount-values-of-dict-like-object-in-multiple-files%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
1
Does the last entry in each object actually have a comma at the end? If so, neither input nor output is valid JSON.
– Kusalananda♦
2 days ago
Yes, each entry does have a comma at the end.
– StatsSorceress
2 days ago
1
Python has a good json package that would make short work of this.
– stolenmoment
2 days ago
Except each input file is not in json format; they're txt files. Does that matter?
– StatsSorceress
2 days ago
@StatsSorceress There can be various nice solutions through command line, but are you sure you wouldn't like a python solution ? that format is exactly python's format for dict literals and those files can be easily slurped in by a very short and simple python script
– LL3
2 days ago