Grayson Henry
Grayson Henry

Reputation: 862

Parse string from file and set to variable - Shell script

I have a text file with a single line:

Our States M0->M1: 369 M0<-M1: 974 M1->M2: 165 M0<-M2: 164

I am trying to set a variable equal to what M0->M1 is. In this case it would be 369.

Currently what I have so far:

file="stats.txt"

while read line
do
    initial_m2=`grep "M0->M3 [0-9]+" $file`
    echo "$line"
done <"$file"

This prints:

(nothing - blank line)
Our States M0->M1: 369 M0<-M1: 974 M1->M2: 165 M0<-M2: 164

Any help would be greatly appreciated.

Upvotes: 0

Views: 1297

Answers (1)

William Pursell
William Pursell

Reputation: 212188

If you're just tyring to read the 4th column:

while read one two three initial_m2 blah; do
  echo $initial_m2
done < $file

Upvotes: 1

Related Questions