2016-02-09 121 views
0
//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「<」,「>」的之間的字符串。我如何去做這件事?隔離的文本文件的某些部分與shell腳本

我使整個並祝字符串

translator=$(<config.txt) 

我想iscolate在命令部分的一切,並將其存儲爲「命令翻譯」的變量。

從這一點來說我打算分割的每一行,每一組的命令是這樣的:

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

我太笨,請幫幫我!

+3

請編輯您Q可顯示您預期的輸出給您的樣本數據。祝你好運。 – shellter

+1

請同時嘗試更好地解釋你的意思*隔離* –

+1

一個簡短的'awk'腳本可以做到這一點,但首先你必須研究'awk'。 – chepner

回答

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

用於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 = [] 
----