我有一个这种格式的文本文件:
public class Simple_AI_patrol : MonoBehaviour {
public Transform[] wayPoints;
public float speed;
public int currentPoint;
public bool doPatrol = true;
Vector3 velocity;
Vector3 moveDirection;
Vector3 target;
Rigidbody rb;
void Start () {
rb = GetComponent<Rigidbody> ();
}
void FixedUpdate () {
if(currentPoint < wayPoints.Length){
target = wayPoints [currentPoint].position;
moveDirection = target - transform.position;
velocity = rb.velocity;
if(moveDirection.magnitude < 1){
currentPoint = Random.Range (0, wayPoints.Length);// or currentPoint++
}else{
velocity = moveDirection.normalized * speed;
}
}else{
if(doPatrol){
currentPoint = 0;
}else{
velocity = Vector3.zero;
}
}
rb.velocity = velocity;
transform.LookAt (target);
}
}
我需要在每一行读取2016080100和2016080100并将其放入变量中。 我怎么能在Linux中做到这一点?
答案 0 :(得分:1)
这是基于mwp的答案,只是稍微调整一下以自动导出到变量中,但这假设你的第一列是唯一的,第三列总是以d =
开头`cat file.txt | awk -F: '{print "export var"$1$3}'`
它会根据你的例子将它们导出到var1d和var2d:
echo $var1d
2016080100
echo $var2d
2016080100
答案 1 :(得分:0)
有很多方法可以做到这一点。您可以使用awk
拆分冒号线并选择所需的行(按第一列),然后再次使用awk
将第三个字段拆分为等号以获取日期。要将结果作为变量存储在shell脚本中,请使用带反引号``
或$()
的命令替换。
# Select the row where the first column is equal to "1".
FOO=$(awk -F: '$1 == "1" { print $3 }' </path/to/your/file | awk -F= '{ print $2 }')
# Select the row where the first column is equal to "2".
BAR=$(awk -F: '$1 == "2" { print $3 }' </path/to/your/file | awk -F= '{ print $2 }')
答案 2 :(得分:0)
假设您的文件data.txt
是:
1:0:d=2016080100:REFC:entire atmosphere:anl
2:415003:d=2016080100:RETOP:cloud top:anl
并且您想要创建一个包含等号后面的所有值的变量(数组)。然后你可以这样做:
d=($(sed -n 's/.*=\([^:]*\).*/\1/p' data.txt))
验证捕获的数据是否符合您的要求:
echo ${d[@]}
2016080100 2016080100
命令说明:
d=($(sed -n 's/.*=\([^:]*\).*/\1/p' data.txt))
's/ ' data.txt substitution command with data.txt as input
.*=\([^:]*\).*/\1/ keep only number after equal sign
sed -n /p print replaced text
d=($( )) get output of sed and store in array d