Question

In: Computer Science

(BASH) Say I have an array of strings with about 100 numbers ranging in length from...

(BASH) Say I have an array of strings with about 100 numbers ranging in length from 3-6 that represent some sort of ID. Within the array of strings, there are some duplicates numbers. What I am trying to do is represent only the top 12 numbers in the form (ID, count occurence) where its ranked by the count occurrences. So the first number would not be the ID with the most digits but it would be the one with the most occurrences.  Is there a way to do this with AWK , begin? or how could i approach this problem?

Solutions

Expert Solution

Solution:

  • Convert given array of strings to a string

For example, for array

declare -a arr=("ABS" "ABC" "SAF" "ABS" "ABC" "SAF" "ABS" "ABC" "SAF" "SAF")

string is str = "ABS ABC SAF ABS ABC SAF ABS ABC SAF SAF"

  • Use below command that count occurence and then sort and print top 12 results.

Command: echo $str | tr '[:space:]' '[\n*]' | sort | uniq -c | sort -bnr | head -12

where,

  • echo: print the result
  • tr: just replaces spaces in string with newlines
  • sort: to prepare input for uniq
  • uniq -c: to count occurences
  • sort -bnr: sorts in descending order
  • head -12: prints top 12 entries

Complete bash code:

str=""
## declare an array variable
declare -a arr=("ABS" "ABC" "SAF" "ABS" "ABC" "SAD" "ABS" "ABC" "SAF" "SAF" "ABS" "ABC" "SAG" "ABD" "DBC" "CAF" "CAF" "ABD" "SDF" "SSF" "ABS" "ABC" "SAF" "ABS" "ABC" "SRF" "ACS" "ABD" "SRF" "SEF")

## loop through the above array and convert to string str
for i in "${arr[@]}"
do
   str="${str} $i"
done

## Find top 12 id with highest count occurence

echo $str | tr '[:space:]' '[\n*]' | sort | uniq -c | sort -bnr | head -12 | awk '{print $2","$1}'

Output:

ABS,6
ABC,6
SAF,4
ABD,3
SRF,2
CAF,2
SSF,1
SEF,1
SDF,1
SAG,1
SAD,1
DBC,1

Attached screenshot of code:

PS: Let me know if you have any doubt.


Related Solutions

Given an array A of n distinct numbers, we say that a pair of numbers i,...
Given an array A of n distinct numbers, we say that a pair of numbers i, j ∈ {0, . . . , n − 1} form an inversion of A if i < j and A[i] > A[j]. Let inv(A) = {(i, j) | i < j and A[i] > A[j]}. Define the Inversion problem as follows: • Input: an array A consisting of distinct numbers. • Output: the number of inversions of A, i.e. |inv(A)|. Answer the following:...
I have an 8 by 8 array that has values ranging from 1-64. I am wanting...
I have an 8 by 8 array that has values ranging from 1-64. I am wanting to display the array evenly but due to some digits being single and others double, it displays lopsided. What would be the best way to get everything in the array to display in a nice even square?
i have an array of strings, how do i sort them into a binary tree? C...
i have an array of strings, how do i sort them into a binary tree? C Program
Given an array A of n distinct real numbers, we say that a pair of numbers...
Given an array A of n distinct real numbers, we say that a pair of numbers i, j ∈ {0, . . . , n−1} form an inversion of A if i < j and A[i] > A[j]. Let inv(A) = {(i, j) | i < j and A[i] > A[j]}. Answer the following: (a) How small can the number of inversions be? Give an example of an array of length n with the smallest possible number of inversions. (b)...
An abs-sorted array is an array of numbers in which |A[i]| <= |A[j]| whenever i <...
An abs-sorted array is an array of numbers in which |A[i]| <= |A[j]| whenever i < j. For example, the array A = [-49, 75, 103, -147, 164, -197, -238, 314, 348, -422], though not sorted in the standard sense, is abs-sorted. Design an algorithm that takes an abs-sorted array A and a number k, and returns a pair of indices of elements in A that sum up to k. For example if k = 167 your algorithm should output...
Using Python, generate 100 random numbers ranging from 1 to 5, then plot them using matplotlib...
Using Python, generate 100 random numbers ranging from 1 to 5, then plot them using matplotlib or Seaborn as a histogram
How many bit strings of length 8 if i. bit strings start with the bit 1;...
How many bit strings of length 8 if i. bit strings start with the bit 1; ii. bit strings end with the two bits 00; iii. bit strings either start with the bit 1 or end with the bits 00.؟
Given an array of positive numbers and a positive number S, find the length of the...
Given an array of positive numbers and a positive number S, find the length of the smallest contiguous subarray whose sum is greater than or equal to S. Return 0, if no such subarray exists. Example 1 Input: [2, 1, 5, 2, 3, 2], S=7 Output: 2 Explanation: The smallest subarray with a sum great than or equal to '7' is [5, 2]. Example 2 Input: [2, 1, 5, 2, 8], S=7 Output: 1 Explanation: The smallest subarray with a...
You are given an array of length n that is filled with two symbols (say 0...
You are given an array of length n that is filled with two symbols (say 0 and 1); all m copies of one symbol appear first, at the beginning of the array, followed by all n − m copies of the other symbol. You are to find the index of the first copy of the second symbol in time O(logm). include: code,  proof of correctness, and runtime analysis  
Say I have and Array so MyArray starts off as S A Q W A R...
Say I have and Array so MyArray starts off as S A Q W A R .... I want to sort the Array alphabetically using ascii codes to sort. However in Ascii code capital letters come first and then lower case letters are sorted after. My goal is to not only sort the array, but also make lowercase sorted in its proper place. I am using visual basics and I cant use a built in sorter that does it the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT