logo
down
shadow

Bash - Count number of occurences in textfile and display in descending order


Bash - Count number of occurences in textfile and display in descending order

By : garyfairhead
Date : October 17 2020, 03:08 PM
this one helps. You can use tr with a composite string of the letters you wish to delete.
Example:
code :
$ echo "abc, def. ghi! boss-man" | tr -d ',.!'
abc def ghi boss-man
$ echo "abc, def. ghi! boss-man" | tr -d [:punct:]
abc def ghi bossman
$ echo "one two one! one. oneone
two two three two-one    three" | 
 awk 'BEGIN{RS="[^[:alpha:]]"} 
     /[[:alpha:]]/ {seen[$1]++} 
     END{for (e in seen) print seen[e], e}' | 
 sort -k1,1nr -k2,2
4 one
4 two
2 three
1 oneone


Share : facebook icon twitter icon
Count all div and add each number inside span in descending order

Count all div and add each number inside span in descending order


By : Sourav Tendulkar
Date : March 29 2020, 07:55 AM
help you fix your problem I have this code from earlier question
code :
$("div span").text(function(i) {
    return $("div span").length-i;
});
Count numpy array mixed data types and display in descending order

Count numpy array mixed data types and display in descending order


By : Anonymous
Date : March 29 2020, 07:55 AM
like below fixes the issue In Python 2.7, numpy.unique should work. (Because of the mixed types in your object array, numpy.unique won't work in Python 3; see below for a work-around.) For example, in the following, values is an array containing the unique values in a, and counts is a corresponding array holding the number of times the value occurs in a.
code :
In [31]: a
Out[31]: 
array([[123, '40381', 'V5856', 0.0],
       [456, '40381', '25081', 0.0],
       [789, 'V5856', '51851', 0.0]], dtype=object)

In [32]: values, counts = np.unique(a, return_counts=True)

In [33]: values
Out[33]: array([0.0, 123, 456, 789, '25081', '40381', '51851', 'V5856'], dtype=object)

In [34]: counts
Out[34]: array([3, 1, 1, 1, 1, 2, 1, 2])
In [35]: order = counts.argsort()[::-1]

In [36]: values[order]
Out[36]: array([0.0, 'V5856', '40381', '51851', '25081', 789, 456, 123], dtype=object)

In [37]: counts[order]
Out[37]: array([3, 2, 2, 1, 1, 1, 1, 1])
In [23]: a
Out[23]: 
array([[123, '40381', 'V5856', 0.0],
       [456, '40381', '25081', 0.0],
       [789, 'V5856', '51851', 0.0]], dtype=object)

In [24]: values, counts = np.unique(a.astype(str), return_counts=True)

In [25]: values
Out[25]: 
array(['0.0', '123', '25081', '40381', '456', '51851', '789', 'V5856'], 
      dtype='<U5')

In [26]: counts
Out[26]: array([3, 1, 1, 2, 1, 1, 1, 2])

In [27]: order = counts.argsort()[::-1]

In [28]: values[order]
Out[28]: 
array(['0.0', 'V5856', '40381', '789', '51851', '456', '25081', '123'], 
      dtype='<U5')

In [29]: counts[order]
Out[29]: array([3, 2, 2, 1, 1, 1, 1, 1])
I'm trying to count all letters in a txt file then display in descending order

I'm trying to count all letters in a txt file then display in descending order


By : Mehdi Mido
Date : March 29 2020, 07:55 AM
This might help you Displaying in descending order needs to be outside your search-loop otherwise they will be displayed as they are encountered.
Sorting in descending order is quite easy using the built-in sorted (you'll need to set the reverse-argument!)
code :
from collections import Counter
from operator import itemgetter

def frequencies(filename):
    # Sets are especially optimized for fast lookups so this will be
    # a perfect fit for the invalid characters.
    invalid = set("‘'`,.?!:;-_\n—' '")

    # Using open in a with block makes sure the file is closed afterwards.
    with open(filename, 'r') as infile:  
        # The "char for char ...." is a conditional generator expression
        # that feeds all characters to the counter that are not invalid.
        counter = Counter(char for char in infile.read().lower() if char not in invalid)

    # If you want to display the values:
    for char, charcount in sorted(counter.items(), key=itemgetter(1), reverse=True):
        print(char, charcount)
How to display the serial number of the table rows in descending order in php?

How to display the serial number of the table rows in descending order in php?


By : user2708527
Date : March 29 2020, 07:55 AM
seems to work fine If you already have loop outputting the 1-10 version, you could simple change the output to show 11 minus the current count...
code :
echo "<td>".(11-$i)."</td>";
$i= 11;
while($i>0)
{
    $i--;
    echo "<td>".$i."</td>";
}
Sorting a textfile that contains player name and score in descending order

Sorting a textfile that contains player name and score in descending order


By : user3425926
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I am trying to open a text file and then rearrange it in descending order, to show who has the highest score. In the text file there's the player name and their score. , Assuming your text file looks like this:
code :
Name1 1
Name2 1
Name4 5
Name3 6
#include <string>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <functional>
#include <vector>


int main()
{
    std::string line;
    std::ifstream inFile;
    inFile.open("/C:/Users/kkpet/Desktop/highscore.txt");
    if (inFile.is_open()) {
        std::vector<std::pair<int, std::string> > score_vector;
        std::string name;
        int score;
        while (inFile >> name >> score) {
            score_vector.push_back(std::make_pair(score, name));
            std::cout << line << '\n';
        }
        inFile.close();
        std::sort(score_vector.begin(), score_vector.end());
        std::reverse(score_vector.begin(), score_vector.end());
        for(auto it = score_vector.begin(); it != score_vector.end(); ++it){
            std::cout << "Name: " << it->second << " Score: " << it->first << std::endl;
        }
    }
    else
        std::cout << "Unable to open text";
}
#include <string.h>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <functional>
#include <vector>

// This is a compare funciton for the sort algorithm of std::sort. See [1]
bool compareScorePair(std::pair<std::string, int>&a, std::pair<std::string, int>&b){
    if(a.second > b.second){return true;}
    if(a.second == b.second){return a.first.compare(b.first) > 0;}
    return false;
}

int main()
{
    std::ifstream inFile;
    inFile.open("C:/Users/kkpet/Desktop/highscore.txt");
    if (inFile.is_open()) {
        std::vector<std::pair<std::string, int> > score_vector;
        std::string name;
        int score;
        while (inFile >> name >> score) { // Can be used to directly assign istream data to variables. See [2]
            score_vector.push_back(std::make_pair(name, score)); // Storing data as pair, to keep relationships between score and name.
        }
        inFile.close();
        std::sort(score_vector.begin(), score_vector.end(), compareScorePair); // Sort the vector with the custom compare function, See top of code.
        int place = 1;
        //auto is used purely for convenience. auto represents std::vector<std::pair<std::string, int> >::iterator here.
        for(auto it = score_vector.begin(); it != score_vector.end(); ++it){ 
            std::cout << "Place: " << place << " Name: " << it->first << " Score: " << it->second << std::endl;
            ++place;
        }
        // The whole for loop could look like this:
        /*
        for(uint i = 0; i < score_vector.size(); ++i){
            std::string name_out = score_vector[i].first;
            int score_out = score_vector[i].second;
            std::cout << "Place: " << i << " Name: " << name_out << " Score: " << score_out << std::endl;
        }
        */
    }
    else
        std::cout << "Unable to open text";
}
Place: 1 Name: Name3 Score: 6
Place: 2 Name: Name4 Score: 5
Place: 3 Name: Name2 Score: 1
Place: 4 Name: Name1 Score: 1
Related Posts Related Posts :
  • Innobackupex call from bash script
  • How to remove lines appear only once in a file using bash
  • Bash Script Function
  • Search for numbers in files and replace the match with "result -1"
  • Rename file based on content
  • Insert part of filename to beginning of lines within file
  • How to backup an application's database
  • alias for cp with arguments
  • For loop deletes files Instead of renaming
  • Execute ssh shell script from npm command
  • Exclude certain string bash
  • Bash test value evaluation
  • Running bash script with parameter
  • Bash: find files within file size range and report their names to a file
  • I have many single files nested within many folders. Is there a way in Mac OSX to extract all the files into one folder?
  • Create a note in privnote.com using HTTP requests
  • Recursively find hexadecimal bytes in binary files
  • How to kill a background job run by bash file
  • How to strip or replace all matches of a pattern from a string in Bash?
  • How can I run executable in shell script?
  • How do I pass BASH host command a variable and assign the answer to another variable
  • Change permissions of file in CMake
  • update stored variables in shell script
  • Grep + sed combination
  • Find a line with certain string then remove it's newline character at the end in bash
  • shadow
    Privacy Policy - Terms - Contact Us © soohba.com