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
  • 741,623
  • 53
  • 500
  • 612
MevlütÖzdemir
  • 3,180
  • 1
  • 23
  • 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
  • 14,455
  • 5
  • 68
  • 83
Barmar
  • 741,623
  • 53
  • 500
  • 612
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
  • 761,203
  • 64
  • 569
  • 643