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

2
  • 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, 2014 at 0:35
  • 66(oops)6 does not count as 6. it will count only if you will input number 66(oops)6 Mar 11, 2014 at 0:42

3 Answers 3

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
7
  • Can you explain to me pleas what @ means in array[@]. and what expr meains? Thanks Mar 11, 2014 at 0: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, 2014 at 0:53
  • @user3127680 expr is a command that returns the results of an arithmetic expression. For more info, see the man page.
    – Blue Ice
    Mar 11, 2014 at 0: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. Mar 11, 2014 at 1:05
  • 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. Mar 11, 2014 at 2: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
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
2
  • 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. Mar 11, 2014 at 0: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 :) Mar 11, 2014 at 0:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.