壳解析线寻找一个特定的标记 [英] shell parsing a line to look for a certain tag
问题描述
我打算创建一个简单的脚本编辑基于存储属性文件中值的文件。
所以基本上,我通过在原文件中的每一行计划循环,当遇到特定标记行内说的/#,它将获取文本以下标签即证书,然后实现一个功能通过属性来解析文件以获取一定的值,并将其添加到原始文件。
因此,例如,该文件将有以下行:
I am planning to create a simple script to edit a file based on values stored within a properties file. So essentially I am planning to loop through each line in the original file, when it comes across a certain tag within a line say "/#" it will get the text following the tag i.e. certs and then implement a function to parse through the properties file to get certain values and add them to the original file. So for example the file would have the following line:
/#证书
我不知道如何最好地搜索标签,我正打算有一个,如果找到/#,然后平分剩余的文本得到的字符串。
I am not sure how best to search for the tag, I was planning to have an if to find the /# and then split the remaining text to get the string.
while read line
do
#need to parse line to look for tag
echo line >> ${NEW_FILE}
done < ${OLD_FILE}
任何帮助会Ëpciated大大AP $ P $
Any help would e greatly appreciated
=====================================
=====================================
编辑:
我的解释是有点差;道歉。我只是想获得/#以下文字 - 即我只是想获得的字符串值precedes它。然后我可以调用基于文本就是一个函数。
My explanation was a bit poor; apologies. I am merely trying to get the text following the /# - i.e. I just want to get the string value that precedes it. I can then call a function based on what the text is.
推荐答案
这是移植到Bourne shell的,因此,当然,KSH和Bash。
This is portable to Bourne shell and thus, of course, ksh and Bash.
case $line in
'/#'* ) tag="${line#/\#}" ;;
esac
要放入一些上下文,这里是如何使用它一个更现实的例子:
To put it into some sort of context, here is a more realistic example of how you might use it:
while read line; do
case $line in
'/#'* ) tag="${line#/\#}" ;;
*) continue ;; # skip remainder of loop for lines without a tag
esac
echo "$tag"
# Or maybe do something more complex, such as
case $tag in
cert)
echo 'We have a cert!' >&2 ;;
bingo)
echo 'You are the winner.' >&2
break # terminate loop
;;
esac
done <$OLD_FILE >$NEW_FILE
这篇关于壳解析线寻找一个特定的标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!