我有一个包含单行的文本文件:
Our States M0->M1: 369 M0<-M1: 974 M1->M2: 165 M0<-M2: 164
我试图设置一个等于 M0->M1 的变量。在这种情况下,它将是 369。
目前我所拥有的:
file="stats.txt"
while read line
do
initial_m2=`grep "M0->M3 [0-9]+" $file`
echo "$line"
done <"$file"
这打印:
(nothing - blank line)
Our States M0->M1: 369 M0<-M1: 974 M1->M2: 165 M0<-M2: 164
如有任何帮助,我们将不胜感激。
最佳答案
如果您只是厌倦了阅读第 4 列:
while read one two three initial_m2 blah; do
echo $initial_m2
done < $file
关于linux - 从文件中解析字符串并设置为变量 - Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25735398/