0

I have a script where I write the score to a file, named log.txt

In this file I save the score like this: (number 1 is just a example)

Won: 1
Lose: 1

I've written this AWK command:

gameswon=`awk -F : '{print $2}' "$file"`

It gives me this result:

1 1

How can I save the first number to, "won" And the second number to "lose"

Hope anyone can help me

Barmar
  • 669,327
  • 51
  • 454
  • 560
MevlütÖzdemir
  • 2,741
  • 1
  • 21
  • 28

2 Answers2

1

You can use a bash array:

gameswon=($(awk -F: '{print $2}' "$file"))
won=${gameswon[0]}
lose=${gameswon[1]}

This puts the output of awk -F: '{print $2}' "$file" into the array $gameswon

Adam Katz
  • 12,301
  • 3
  • 59
  • 74
Barmar
  • 669,327
  • 51
  • 454
  • 560
0

You can use read with little modifier awk in process substitution:

read -r won lose < <(awk -F : '$1 ~ /^(Won|Lose)$/{printf "%s ", $2+0}' "$file")
anubhava
  • 713,503
  • 59
  • 514
  • 593