I'm trying to make a script that takes in a log file where each line consists of a name + >tab< + runtime (number), like this:
calloffice 14
name 15
other 16
CallOffice 18
What i want to do is to find all lines that match a given name and then add together the runtime numbers.
So i started with getting them to print out in the first place. But my script should be able to search for a keyword in the logg file and get all the different runtimes from ONLY that keyword, now I get the runtime from ALL the numbers instead of only the keywords runtime.
this is what i have done so far:
#!/bin/bash
echo "What is the event?"
FILE=$1
while read text
do
if grep "$text" hendelse.txt; then
$text | cut -f2 -d$'\t' hendelse.txt
else
echo "text not found"
fi
done
I know I'm not even near the finish line in the script, but my question was just how do I get the runtime numbers from the specific keyword?
Per the original question and bits-n-pieces extracted from OP's comments:
Example input file:
$ cat test.txt
calloffice 14
name 15
other 16
CallOffice 18
One possible awk
solution:
BEGIN {total=0}
tolower($1)==str {total+=$2}
END {printf "total : %s\n",total}
BEGIN {total=0}
: initialize our totaltolower($1)==str
: lower case field #1 (allows for case-insensitive matching), and if equal to our input string str
then ...total+=$2
: increment our total by the amount in field #2And the awk
script in action when passing in a search string of 'calloffice':
$ awk '
BEGIN {total=0}
tolower($1)==str {total+=$2}
END {printf "%s total : %s\n",str,total}
' str="calloffice" test.txt
calloffice total : 32