Reputation: 15
I have a list of lines of the form
file:starting linenumber:starting column number:ending linenumber:ending column number
That specify a region of text in given files; for example
/home/user/Desktop/helloworld.c:10:5:10:15
What is an efficient way of displaying on screen the corresponding line(s) in helloworld.c? I am pretty sure it is doable with head/tail, but am not too sure about its performance on larger files. If there is a way to also 'prettyprint' the region of text by colouring, that would be great to know about too.
Upvotes: 0
Views: 118
Reputation: 27285
Given a single line of the format file:startRow:startCol:endRow:endCol
use the following bash function to print the referenced lines and highlight the referenced text:
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"
}
Example usage:
seq -w 1 10000 > file
highlight file:2:4:5:1
prints
To process a list of / file with multiple file:startRow:startCol:endRow:endCol
use the function inside a loop:
IFS= read -r line; do
highlight "$line"
done < list
This is not the most efficient approach, since the same file might be read multiple times. However, it probably is efficient enough. I doubt you would notice any delays, even with a lot of large files.
Upvotes: 1