簡體   English   中英

如何在給定行號和文件路徑的終端上顯示文本?

[英]How do I display text on the terminal given line number and filepath?

我有表格的行列表

file:starting linenumber:starting column number:ending linenumber:ending column number

指定給定文件中的文本區域; 例如

/home/user/Desktop/helloworld.c:10:5:10:15

在屏幕上顯示 helloworld.c 中相應行的有效方法是什么? 我很確定它可以用於頭/尾,但不太確定它在較大文件上的性能。 如果有一種方法也可以通過着色來“漂亮地打印”文本區域,那也很高興知道。

給定格式file:startRow:startCol:endRow:endCol使用以下 bash function 打印引用的行並突出顯示引用的文本:

highlight() {
    IFS=: read -r file srow scol erow ecol <<< "$1"
    shl=$'\033[31m' # start highlight, in this case red text
    ehl=$'\033[0m'  # end highlight, in this case normal text
    sed -n "$erow s/./&$ehl/$ecol;$srow s/./$shl&/$scol;$srow,$erow p;$erow q" "$file"
}

示例用法:

seq -w 1 10000 > file
highlight file:2:4:5:1

印刷

輸出:突出顯示的文本

要處理具有多個file:startRow:startCol:endRow:endCol的 / 文件列表,請在循環內使用 function:

IFS= read -r line; do
    highlight "$line"
done < list

這不是最有效的方法,因為可能會多次讀取同一個文件。 但是,它可能足夠有效。 我懷疑你會注意到任何延遲,即使有很多大文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM