0

I want to find number or words in file. As first parameter it gets file name and second number you are looking for.

For example I write in command line:

bash script.sh file.txt 6

And i get on output

Number 6 repeats 4 time

This is content in file.txt

5 4 5 6 2 4 6 3 6 6

This is the code what I came up and stuck

para2=$2
while read line
do
    array=($line)
    echo "Value of third element in my array : ${array[3]} "
done < $1

I dont know how to compare parameter 2 with every array. I know that in code above I print out third array but I dont know how to go through every array and compare them with parameter two. I mean i want to go through all numbers and compare with input parameter. Pleas help

user3127680
  • 353
  • 2
  • 4
  • 13
  • does 66(oops)6, count as 3 6's or 66 and 1 6? Sorry to say, but your sample code `echo "value of third "... doesn't seem to match your stated objective. Good luck. – shellter Mar 11 '14 at 00:35
  • 66(oops)6 does not count as 6. it will count only if you will input number 66(oops)6 – user3127680 Mar 11 '14 at 00:42

3 Answers3

0

Try this:

numOccurences=0

while read line
do
    array=($line)

    for i in "${array[@]}"
    do
       if [ "$2" = "$i" ]
       then
           numOccurences=`expr $numOccurences + 1`
       fi
    done
done < $1

echo "$2 occurs $numOccurences times in $1"

The program will read a line, iterate through the array formed by the line, and then it will compare the value to the target character. A counter is updated for every match, and the result is printed at the end.

Example input (file.txt):

5 4 5 6 2 4 6 3 6 6 6 6
6 6
᠎

Command:

/Users/Robert/Desktop/Untitled.sh /Users/Robert/Desktop/file.txt 6

Output:

6 occurs 8 times in /Users/Robert/Desktop/file.txt
Blue Ice
  • 7,888
  • 6
  • 32
  • 52
  • Can you explain to me pleas what @ means in array[@]. and what expr meains? Thanks – user3127680 Mar 11 '14 at 00:48
  • @user3127680 The `@` in `array[@]` returns all of the values in `array`- that is, `5 4 5 6 2 4 ...`. The loop makes `i` progress through these values, so in the first iteration, `i = 5`, then in the second iteration, `i = 4`, then `i = 5`, then `i = 6`, etc. – Blue Ice Mar 11 '14 at 00:53
  • @user3127680 `expr` is a command that returns the results of an arithmetic expression. For more info, see the [man page](http://ss64.com/bash/expr.html). – Blue Ice Mar 11 '14 at 00:55
  • Thank you. You helped me a lot. Do you maybe know how to check if parameter one is realy a file. For example if you put in script.sh 6 you get some echo this is not a file. – user3127680 Mar 11 '14 at 01:05
  • @user3127680 See [here](http://stackoverflow.com/questions/638975/how-do-i-tell-if-a-file-does-not-exist-in-bash). It sounds like you can figure out how to put it in the code. :) – Blue Ice Mar 11 '14 at 01:09
  • I did. Thanks for everything! – user3127680 Mar 11 '14 at 01:14
  • 1
    @user3127680 Don't forget to mark the answer if it helped you. You can click the tick mark next to the answer so that it's green. – jaypal singh Mar 11 '14 at 02:55
0
para2=$2
counter=0
while read line
do
    for num in $line
    do
        if [[ $num -eq $para2 ]]
        then let counter = ((counter + 1))
        fi
    done
done < "$1"
echo Number $para2 repeats $counter times
Barmar
  • 741,623
  • 53
  • 500
  • 612
0
#!/bin/bash

    echo "Number to be searched $2 "
    echo "File name passed : $1"

filename=$1
count=0

while read line
do
   for word in $line; do
        #echo "Number = $word"
        if [ "$2" == "$word" ]; then
           count=$(expr $count + 1)
        fi
    done
done < $filename

echo $2 is observed $count times
Mayur Nagekar
  • 813
  • 5
  • 13
  • You can access the passed parameters directly with $1,$2 in your script make sure you know the position of them since they are positional parameters. As you asked for, expr is for performing mathematical calculations, it's inbuilt in math tool. – Mayur Nagekar Mar 11 '14 at 00:48
  • Since you want to scan the numbers/words in the line itself and not line by line, you can loop through the words in line when you read the file and then match the number with what is read from the file and keep incrementing the counter. Simple ? Any more questions/queries, let me know :) – Mayur Nagekar Mar 11 '14 at 00:53