//unit-translator
#head
<
shell: /bin/bash;
>
#stuffs
<
[~]: ~;
[binary's]: /bin/bash;
[run-as-root]: sudo;
>
#commands
<
make-directory:mkdir;
move-to-directory:cd;
url-download-current-dirrectory:wget;
extract-here-tar:tar;
copy:cp;
remove-directory-+files:rm -R;
enter-root:su;
>
我希望在“#commands”之后隔离所有内容,在2“&lt;”,“&gt;”之间作为字符串。我该怎么做?
我把整个fille变成了一个字符串
translator=$(<config.txt)
我想在命令部分中对所有内容进行iscolate,并将其存储为变量“translator commands”。
从那时起,我计划拆分每一行,每组命令都是这样的:
IFS=';' read -a translatorcommandlines <<< "$translatorcommands"
IFS=':' read -a translatorcommand <<< "$translatorcommandlines"
我很无能,请帮助我!
答案 0 :(得分:2)
如果您要在#command
和<
之间>
之后提取所有行,则可以使用此命令:
sed '0,/^#command/d' config.txt | sed '/>/q' | grep "^\w"
跳过#command
之前的所有行,打印行直到>
,并且仅采用以字符开头的行。
我的文件输出是:
make-directory:mkdir;
move-to-directory:cd;
url-download-current-dirrectory:wget;
extract-here-tar:tar;
copy:cp;
remove-directory-+files:rm -R;
enter-root:su;
答案 1 :(得分:1)
UNIX的通用文本处理工具是“awk”。你没有在你的问题中显示你希望你的输出是如此idk你想要什么,但希望这足以让你从这里弄清楚:
$ cat tst.awk
BEGIN { RS=">"; FS="\n" }
{ gsub(/^.*<[[:blank:]]*\n|\n[[:blank:]]*$/,"") }
NF {
for (i=1;i<=NF;i++) {
print "record", NR, "field", i, "= [" $i "]"
}
print "----"
}
$ awk -f tst.awk file
record 1 field 1 = []
record 1 field 2 = [shell: /bin/bash;]
record 1 field 3 = []
----
record 2 field 1 = []
record 2 field 2 = [[~]: ~;]
record 2 field 3 = [[binary's]: /bin/bash;]
record 2 field 4 = [[run-as-root]: sudo;]
record 2 field 5 = []
record 2 field 6 = []
----
record 3 field 1 = []
record 3 field 2 = [make-directory:mkdir;]
record 3 field 3 = [move-to-directory:cd;]
record 3 field 4 = [url-download-current-dirrectory:wget;]
record 3 field 5 = [extract-here-tar:tar;]
record 3 field 6 = [copy:cp;]
record 3 field 7 = [remove-directory-+files:rm -R;]
record 3 field 8 = [enter-root:su;]
record 3 field 9 = []
----