使用 shell 脚本隔离文本文件的某些部分

Isolate certain parts of a text file with shell script

//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 个“<”、“>”之间的所有内容隔离为一个字符串。我该怎么做?

我把整个圆做成了一个字符串

translator=$(<config.txt)

我想隔离命令部分中的所有内容,并将其存储为变量 "translator commands"。

从那时起,我计划拆分每一行,每组命令如下所示:

IFS=';' read -a translatorcommandlines <<< "$translatorcommands"
IFS=':' read -a translatorcommand <<< "$translatorcommandlines"

我很无能,请帮帮我!

如果您想提取 #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;

用于 UNIX 的通用文本处理工具是 "awk"。你没有在你的问题中显示你想要你的输出是什么所以我知道你想要什么但希望这足以让你从这里弄清楚:

$ 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 = []
----