我有以下输入
- MyComposite [2.1],partition = default,mode = active,state = on,isDefault = true,deployedTime = 2012-05-07T15:35:22.473-07:00
- MessageManager [1.0],partition = default,mode = active,state = on,isDefault = true,deployedTime = 2012-05-07T15:37:14.137-07:00
- SimpleApproval [1.0],partition = default,mode = active,state = on,isDefault = true,deployedTime = 2012-05-07T15:28:39.599-07:00
醇>
我有一个脚本从文件中逐行解析输入,但我不知道如何从每行提取单个参数到局部变量,这样我就可以执行其他过程
到目前为止,我正在尝试以下方法:
#!/bin/ksh
file="output"
compositeName="foo" ci=0
# while loop while read line do
# display line or do somthing on $line
if echo "$line" | egrep -q '\[[0-9]*\.[0-9]*\].*?(mode=active).*?
(state=on)' then compositeName=$( echo "$line" | egrep '[0-9]*' )
echo "$compositeName"
#echo "$line"
fi
done <"$file"
我看起来只是从这个字符串中提取两个值,第一个单词和括号之间的浮点数
即:
name = MyComposite version = 2.1
任何想法?
答案 0 :(得分:2)
我不确定这些行号是否在文件中。如果没有,你可以这样做:
#!/usr/bin/env ksh
while IFS="," read nameVersion line; do
name="${nameVersion%%\[*}"
version="${nameVersion//*\[+([0-9.])\]*/\1}"
print "name=$name version=$version"
done < "$file"
如果文件中的行号 ,请将上述脚本中的名称分配更改为name="${nameVersion//+([0-9]).+( )+(*)\[*/\3}"