When I make grep
highlight the matches like this:
echo hello hello | grep --color "hello"
I get highlighted all matches in the line, which in the above case is all the line:
hello hello
How can I get highlighted only the first ocurrence :
hello hello
I suppose I can do it with a complex regex but I wonder if there a simpler solution.
It can be easily done using sed
:
sed 's/hello/\x1b[31m&\x1b[0m/' file
This will only color first match in each line.
Similarly you can do this in awk
as well:
awk '{sub(/hello/, "\x1b[31m&\x1b[0m")} 1' file
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.