猫文件到终端并为特定单词上色

时间:2018-12-25 08:12:42

标签: colors terminal grep cat

我已经阅读了几篇有关语法突出显示的帖子,catgrep(例如this onethis one too),但尚未找到我想要的内容对于。我想将整个文本文件打印到终端,并使用一个特定的(重复的)单词红色。没有单词的行也应该打印出来。这很接近,但仅 打印包含特定单词的行。这篇文章没有显示,但是 load 字样应显示为红色。

$ cat input.ll | grep "load"
  %tmp = load %class.A*, %class.A** %p, align 8
  %vtable = load i32 (%class.A*, i32, i32, i32)**, i32 (%class.A*, i32, i32, i32)*** %tmp1, align 8
  %tmp2 = load i32 (%class.A*, i32, i32, i32)*, i32 (%class.A*, i32, i32, i32)** %vfn, align 8
  %tmp3 = load i32, i32* @x, align 4

这是原始文件:

$ cat input.ll
  %tmp = load %class.A*, %class.A** %p, align 8
  %tmp1 = bitcast %class.A* %tmp to i32 (%class.A*, i32, i32, i32)***
  %vtable = load i32 (%class.A*, i32, i32, i32)**, i32 (%class.A*, i32, i32, i32)*** %tmp1, align 8
  %vfn = getelementptr inbounds i32 (%class.A*, i32, i32, i32)*, i32 (%class.A*, i32, i32, i32)** %vtable, i64 2
  %tmp2 = load i32 (%class.A*, i32, i32, i32)*, i32 (%class.A*, i32, i32, i32)** %vfn, align 8
  %tmp3 = load i32, i32* @x, align 4
  %call = call i32 %tmp2(%class.A* %tmp, i32 3, i32 %tmp3, i32 7)

1 个答案:

答案 0 :(得分:1)

我建议使用GNU grep:

import cv2
import math
import numpy as np

#Read gray image
img = cv2.imread("2.jpg",0)

#Create default parametrization LSD
lsd = cv2.createLineSegmentDetector(0)

#Detect lines in the image
lines = lsd.detect(img)[0] #Position 0 of the returned tuple are the detected lines
print(lines[0][0][0])

ver_lines = []

for line in lines:
    angletan = math.degrees(math.atan2((round(line[0][3],2) - round(line[0][1],2)), (round(line[0][2],2) - round(line[0][0],2))))

    if(angletan > 85 and angletan < 95):
        ver_lines.append(line)

#Draw detected lines in the image
drawn_img = lsd.drawSegments(img,np.array(ver_lines))

#Show image
cv2.imshow("LSD",drawn_img )
cv2.waitKey(0)