Shell scripting

  1. Let’s say you have a string ex. ajjdjjdajjrjj
    I want to check occurance of each letter (a - 2 times , j- 2 times, likewise). And print the count of it. How will you do it.

  2. Let’s say you have numbers 1 to 100 and some numbers are missing in this range, how will you print the missing numbers.

  3. You have numbers 1 to 10, you want to print numbers in reverse order, how will you do that

Hello umesht0001,

1- I would use the following awk command:

string="text,text,text,text"
char=","
awk -F"${char}" '{print NF-1}' <<< "${string}"

I’m splitting the string by $char and print the number of resulting fields minus 1.

If your shell does not support the <<< operator, use echo :

echo "${string}" | awk -F"${char}" '{print NF-1}'

then save every number in an array loop on the array and echo each letter with its number.

2-
you can loop in a range between 1 to 100 set an if condition to check on the number if it exists or not if not add it to an array then print this array.

3- use this example

#!/bin/bash

array=(1 2 3 4 5 6 7)

f() { array=("${BASH_ARGV[@]}"); }

shopt -s extdebug
f "${array[@]}"
shopt -u extdebug

echo "${array[@]}"

Thanks,
KodeKloud Support