Shell scripting task

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:

Hi all,
I am new to shell scripting. And I have a task to do, I tried all possible ways to solve this, but didn't. Iwas wondering maybe someone could help me.

So I have a random file something like this

Andrew John Mike
Alfa Omega Beta

And I need to create scrip witch would filter any file like this:

  • Find two most repeated symbols in file ( in this file I think it would be "a")
  • Find most repeated line end in this file
  • And symbol combination, witch is not in combination of symbols witch i entered
    like this "joh"
    And write all these to new file
    I am really appreciate if someone would help me.
    Thank You.
  1. Relevant commands, code, scripts, algorithms:
    awk, grep

  2. The attempts at a solution (include all code and scripts):
    I tried this:

#!/bin/bash
mas=( Andrew John Mike )
for i in ${#mas[@]}
do
  echo "Emelents count ${#mas[@]}"
  echo ${mas[0]}
done

Well i tried many diffrent ways, I don't know solution for first task, so I can't go any further, cause other tasks are familiar. I just can't go on. Help please .

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    Kaunas College, Kaunas, Lithuania, first course, proffesor R.Krauneviien

${#mas[@]} This returns one single number, so the for-loop will loop once on i=3 then break. I think you meant for STR in "${mas[@]}" to loop over all elements in the array.

If you have to read from a file, start there:

while read -A ARR
do
        echo "${#ARR[@]} elements"
        for STR in "${ARR[@]}"
        do
                ...
        done
done < filename

Then you could just print each STR char by char:

for ((N=0; N<${#STR}; N++)) ; do echo "${STR:$N:1}" ; done

Then pipe the entire outer 'while read' loop through sort, which will make a file like

a
a
a
b
b
b
...

which you can then pipe into uniq -c to count them for you:

18 e
6 a
8 z
...

...then sort | tail -n 2 to get the bottom two.

Not the most efficient solution perhaps, but counting chars is a bit awkward to do in shell.

Ok, I am sorry maybe I am really stupid. I try to do this:

strongid@ktl ~ $ pico sav1

   UW PICO(tm) 4.10                             File: sav1                                 Modified

#!/bin/bash
while read -A band (that's my file)
do
  echo "${#band[@]} elements"
  for STR in "${band[@]}
  do
  done
done
done < savas
                                          [ Wrote 9 lines ]

strongid@ktl ~ $ cat band (not empty)
jonas algis andrius
tomas rimas karolis
saulius justas domas

strongid@ktl ~ $ sh sav1 (try to do this script)
sav1: 5: Syntax error: Bad substitution

So with this error I cant go further to do read file and try to count symbols :frowning:

You're redirecting the file into the entire while loop, not just into 'read'. That <filename at the bottom is where you want to put the file. All read -A band does is tell read to read stdin into the array 'band'.

You've got one too many done's.

If you copy-paste the loop I have it will work(change ... into true), then you can add onto it.

I copy all You written, then:

strongid@ktl ~ $ pico sav1
  
 UW PICO(tm) 4.10                             File: sav1

#!/bin/bash
while read -A ARR
do
    echo "${#ARR[@]} elements"
    for STR in "${ARR[@]}"
    do
    true
    done
done < band

                                           [ Read 9 lines ]

strongid@ktl ~ $ sh sav1
sav1: 5: Syntax error: Bad substitution

I don't exactly understand what ARR means, is it my array from band file?
so it's something like this [jonas algis andrius] ?
[tomas rimas karolis]
[saulius justas domas]

Well, what is line 5? I can't tell from what you pasted where the file begins or ends. #!/bin/bash has to be the very first line or you might not get the bash shell. Also try running it with 'bash filename.sh'

ARR is an array. On the first loop, ARR[0] will be jonas, ARR[1] will be algis, ARR[2] will be andrius.

1 Like

So I done everything You told. Sorry to bother You with my low skills with shell.
Now I have this code

#!/bin/bash
while read -a ARR
do
    echo "${#ARR[@]} elements"
    for STR in "${ARR[@]}"
    do
    true
    done
done < band
for ((N=0; N<${#STR}; N++)); do echo "${STR:$N:1}"; done

Then I bash it:

strongid@ktl ~ $ bash sav1
3 elements
3 elements
3 elements
d
o
m
a
s

So I understand it, it finds 3 elements in every line like 3 words in every line, that's ok, but it takes only last one of these "domas" not all of them in one line and then I can't sort this.

It only does the last line because you didn't put it inside the loop.

1 Like

I figured out where to put sort and etc. thanks.