Reputation: 71
I need to program a football league table with football round results in a text file of this format
abc 4 def 5 ghi 9 hef 10
where format is
[home team][home team points][guest team][guest team points]
And the program will accept five teams and have multiple text files to read. What I don't know is how to get the points of each correspond team. I have seen some solutions on parsing string with one whitespace and delimiter in this site. However, I need to read like this abc 4
def 5
and so on. Is there any solutions?
The following is the code at this moment. I just figuring out how to read correspond scores of a team. Thanks for your kindly help.
if [ $# -eq 0 ]; then
echo "No argument"
else
echo "The number of arguments : $#"
echo "The full list : $@"
myArray=("$@")
echo "${myArray[0]}"
arraylength=${#myArray[@]}
declare -p myArray
#loop for places entered
for ((i=0;i<${arraylength};i++));
do
#iterate on the files stored to find target
for matchfile in match*.txt;
do
declare file_content=$( cat "${matchfile}" )
#check whether a file has target lanaguage
if [[ " $file_content " =~ ${myArray[i]} ]] # please note the space before and after the file content
then
#awk -v a="$file_content" -v b="${myArray[i]}" 'BEGIN{print index(a,b)}'
#echo "${myArray[i]}"
#let j=j+1
echo "${myArray[i]} found in ${matchfile}: with a score ..."
fi
done
done
fi
exit
Upvotes: 0
Views: 334
Reputation: 8617
Since you already have a regex match going with:
if [[ " $file_content " =~ ${myArray[i]} ]]; then
You can adjust it like so:
re="(^| )${myArray[i]} ([0-9]*)( |$)"
if [[ $file_content =~ $re ]]; then
The (^| )
and ( |$)
parts make sure it works if there's space or the start or end of the file after the team name. The ([0-9]*)
part is to capture the score into a "capture group".
Running that regex match will populate the array BASH_REMATCH
with all the matches from the comparison, so ${BASH_REMATCH[2]}
will have the score.
Upvotes: 1