LoginSignup
1
1

More than 5 years have passed since last update.

Python3 > colorText_180707.py > v0.5 > stdin読込み文字列に対してhtmlタグ色付けする > grep -v指定時にキーワードに色付けして閲覧

Last updated at Posted at 2018-07-08
動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04.4 LTS desktop amd64
TensorFlow v1.7.0
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
scipy v0.19.1
geopandas v0.3.0
MATLAB R2017b (Home Edition)
ADDA v.1.3b6
gnustep-gui-runtime v0.24.0-3.1
PyMieScatt v1.7.0
GNU grep 2.25

関連

処理概要

  • テキストをstdinから読込む
  • htmlタグで特定の文字に色付けして標準出力
  • 実行時引数で色付けキーワードの指定

code v0.4

colorText_180707.py
import re
import sys

'''
v0.5 Jul. 08, 2018
  - print Example when keyword is not specified
  - read from stdin
v0.4 Jul. 08, 2018
  - fix bug > color attribute without double quotation
v0.3 Jul. 08, 2018
  - add <BR> for each new line
v0.2 Jul. 08, 2018
  - read command-line arguments
v0.1 Jul. 07, 2018
  - add color html tag for a fixed text ('opt')
'''

# on Python 3.5.2

def print_arguments():
    print('Example:')
    print('    stdin | [cmd] [keyword]')

args = sys.argv
if len(args) == 1:
    print_arguments()
    sys.exit()
if len(args) < 2:
    print('Error: keyword is not specified')
    print_arguments()
    sys.exit()

col_text = args[1]
inRed = '<font color="red">' + col_text + '</font>'

lines = sys.stdin.readlines()
for aline in lines:
    wrk = aline.replace(col_text, inRed)
    print(wrk, end='<BR>')

使用例

$ grep opt *.c | grep -v option | python3 colorText_180707.py opt > tmp.html
$ google-chrome tmp.html 

qiita.png

備考

Chromeでキーワード検索するとそのキーワードが黄色に表示される。
1つの文字列だけを強調する場合はChromeの機能で十分。

2つの文字列をそれぞれ強調したい時に今回のツールは役に立つ。
(例: scatという文字列を赤色表示にして、enumを検索、など)

qiita.png

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1