2016-06-07 43 views
3

我正在寫一個bash腳本,我需要顏色輸出。我有需要時,它顯示在屏幕上被格式化這樣一個文本文件:如何從變量中創建回顯過程顏色命令?

>Barry's Whiskey Glass Drink Cup, XL 10 oz Dri...  $0.75 
example.org/product-page.php?id=1228741 

>Cotton Bath Towel Pool Towel Brown - Easy Car...  $6.11 
example.org/product-page.php?id=1228763 

>Cotton Bath Towel Pool Towel Red - Easy Care ...  $6.11  
example.org/product-page.php?id=1228766 

>Mosiso MacBook Case for iPad Pro 12.9/MacBook...  $1.95 
example.org/product-page.php?id=1228850 

在「> .....」文本必須是一種顏色,「$ ......」另一種顏色,和「example.org ....」淺灰色。我曾經嘗試這樣做:

tac newlist.txt | sed 's/ >/\r\n >/' > bwtext.txt 

    sed -i 's/>/>\\033\[0;34m/g' bwtext.txt 
    sed -i 's/\$/\$\\033\[0;33m/g' bwtext.txt 
    sed -i 's/http/\\033\[0;37mhttp/g' bwtext.txt 

    while read line 
    do 
     echo -e "${line}" 
    done < bwtext.txt 

即插入正確的換碼到文件,但是當呼應出一行行,而不是處理的代碼,它只是打印它們字面上。

>033[0;34mFor Fitbit Alta Accessory Band, Silicone Repl...  $033[0;33m1.97  
033[0;37mexample.org/product-page.php?id=1247364 

>033[0;34mTattify Gradient Nail Wraps - Love Like a Sun...  $033[0;33m0.99  
033[0;37mexample.org/product-page.php?id=1247367 

>033[0;34mEA AromaCare Eucalyptus Essential Oil 120ml/4...  $033[0;33m3.00  
033[0;37mexample.org/product-page.php?id=1247370 

... waiting 10 minutes for next update ... 

我在做什麼錯,或者我該怎麼做對不對?謝謝。

回答

2

您可能不想將控制字符引入純文本文件。見[ exploits of a mom ]
下面是可以解決您的問題的一種方法,不需要sed

#Adding color to bash script output 
while read -r line # -r to prevent mangling of literal backslashes 
do 
    printf '%b\n' "${line/#>/\\e[1;34m>}" #using shell parameter expansion See Ref 2 
    #Also, you need two `\` make one `\` in the substitution 
    printf '%b\n' "\e[0;m" #resetting color to defaults, this is important 
done<your_actual_file 

參考

  1. 擊:[ Using Colors ]
  2. 殼參數[ expansion ]
  3. 在一些符合POSIX標準的shell中,echo即默認echo -e。你可以忽略bash的這一點。查詢[ this ]瞭解更多信息。無論如何,我已經用printf '%b\n'代替echo -e

注:這個答案並不能完全滿足你的要求,但只是說明你做事的一種替代方法。

+0

考慮展示'printf'%b \ n''而不是'echo -e',以便在兼容POSIX的shell上正常運行。 (另外,在'xpg_echo'和'posix'標誌被設置的情況下,甚至bash也不會遵守'echo -e')。 –

+0

另外,讓'while while read -r line'來避免消耗文字反斜槓,從而阻止它們在格式化字符串中被使用。 –

+0

@CharlesDuffy:意見榮幸。 :-) – sjsam

2

您在文件中有正確的轉義序列,但這些序列在while循環中沒有被讀取。嘗試添加原始輸入標誌(-r),同時讀取不會解釋轉義序列並給出所需的格式。

while read -r line 
do 
echo -e "${line}" 
done < bwtext.txt 
+0

這是一個好點。 :-),但是他可以在不向文本文件引入轉義序列的情況下做到這一點。你也可能希望添加一個鏈接到參考。 – sjsam

+0

'echo -e'實際上與POSIX相反 - 不僅僅是擴展,除了在輸出'echo'時在命令行上輸出'-e'以外的任何內容,都違背了標準規定的行爲。因此,如果設置了「posix」和「xpg_echo」標誌,bash將停止執行此操作。符合標準的替代方案是'printf'%b \ n'「$ line」'。請參閱http://pubs.opengroup.org/onlinepubs/009604599/utilities/echo.html,特別是APPLICATION USAGE部分。 –

0

你在哪裏看到033你需要一個文字轉義字符。您的sed和/或echo -e可能可以做到這一點,但它們在這方面沒有標準化,沒有關於您的系統的詳細信息,我們無法告訴您如何修復腳本。但是你可以轉向一個合理標準化的工具,比如Perl;

perl -pe 's/ >/\r\n >/; 
    s/>/>\033[0;34m/g; 
    s/\$/\$\033[0;33m/g; 
    s/http/\033[0;37mhttp/g' newlist.txt 

順便說一句,sed支持將多個命令中的腳本,就像Perl的。即使你做不到這一點,把這些命令放在一個管道中也會比用sed -i反覆重寫同一個文件更有效率。

(此外,[只是替換字符串中的普通字符,不需要反斜槓。在匹配部分,參數是一個正則表達式,其中[當然有特殊含義。換句話說,在s/regex/string/中,regexstring部件受語法略有不同的影響。)

0

我想感謝大家的幫助。我將同時保存-r開關和printf,而不是回憶建議,以備將來參考。我還張貼了這個對Ubuntu的論壇,在那裏我與另一種選擇提供已行之有效:

tac newlist.txt | sed 's/ >/\r\n >/' > reversed.txt 

sed $"s/\(>[^$]*\)\($.*\)/$DESC\1$COST\2/" reversed.txt | sed $"s/\(http.*\)/$LINK\1$CLR/" | sed $"s/\(FREE\)/$FREE\1$CLR/" 

再次感謝大家!