BASH使用grep和awk一个变量中 [英] BASH using grep and awk inside a variable
问题描述
bash-3.2$ FNAME=$1
bash-3.2$ OLD_NO=$(grep "_version=" | awk -F '"' '{print $12}' $FNAME)
2号线似乎并不为我工作。我不能收/正确引用呢?
这似乎挂
Line 2 does not appear to be working for me. Am I not closing/quoting it correctly? It seems to hang
更新脚本,以反映低于建议
Updated the script to reflect below suggestions
echo $OLD_NO
OLD_NO=$(grep '_version=' "$FNAME" | awk -F '"' '{print $12}')
#Get the version of the
echo "What do you want to update release number to?"
REPLACEMENT="_version="$NEW_NO
echo $REPLACEMENT
sed -i ''s/$OLD_NO/$REPLACEMENT/g'' $FNAME
〜
得到一个新的错误
bash-3.2$ ./vu reader.xml
What do you want to update release number to?
_version=
sed: -e expression #1, char 0: no previous regular expression
工作在bash虽然
Works in bash though
bash-3.2$ grep _version market_rules_cd.reader.xml | awk -F '"' '{print $12}'
14.8.21.1
14.8.21.1
推荐答案
行挂起,因为因为你没有通过的grep'_version ='
将阻塞等待标准输入文件名参数。从挂传递文件名grep的,而不是AWK停止。 awk将随后处理的grep的输出:
The line hangs because grep '_version='
will blocking wait on stdin since you didn't passed a file name argument. To stop it from hanging pass the file name to grep, not to awk. awk will then process grep's output:
OLD_NO=$(grep '_version=' "$FNAME" | awk -F '"' '{print $12}')
顺便说一句:工作可以用 AWK
仅完成,你不需要的grep:
Btw: The job can be done with awk
only, you don't need grep:
OLD_NO=$(awk -F '"' '/_version=/ {print $12}' "$FNAME")
请注意有以下形式awk程序:
Note that awk programs having the following form:
condition { action } [; more of them ]
您可以省略情况,如果该行为应适用于每一行(这被广泛使用),但是在这种情况下,你可以使用正则表达式条件的动作仅限制含行 _version =
:
You can omit the condition if the action should apply to every line (this is widely used), however in this case you can use a regex condition to restrict the action only to lines containing _version=
:
/_version=/ { print $12 }
这篇关于BASH使用grep和awk一个变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!