Search code examples
bashshellsh

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-directory:wget;
extract-here-tar:tar;
copy:cp;
remove-directory-+files:rm -R;
enter-root:su;

>

I want to isolate everything after "#commands", between the 2 "<", ">"'s as a string. How do I go about this?

I made the whole fille a string

translator=$(<config.txt)

I want to iscolate everything in the commands section, and store it as the variable "translator commands".

From that point I plan to split each line, and each set of commands something like this:

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

I'm so clueless, please help me!


Solution

  • If you mean to extract all lines after #command between < and > you can go with this command:

    sed '0,/^#command/d' config.txt | sed '/>/q' | grep "^\w"
    

    which skips all lines before #command, prints lines until > and takes only those starting with word character.

    My output for your file is:

    make-directory:mkdir;
    move-to-directory:cd;
    url-download-current-directory:wget;
    extract-here-tar:tar;
    copy:cp;
    remove-directory-+files:rm -R;
    enter-root:su;