Added 'concat' option for appending matches to a file

This commit is contained in:
Jonas Minnberg 2016-09-28 11:48:08 +02:00
parent 021439191c
commit fabdbcaf08
3 changed files with 10 additions and 2 deletions

View File

@ -26,7 +26,7 @@ Each entry consists of several lines.
Each line has form: `keyword=value`
where keyword is one of: regexp, colours, command, skip, replace, count
where keyword is one of: regexp, colours, command, concat, skip, replace, count
Only regexp is mandatory, but it does not have much sense by itself unless you specify at least a colour, skip, replace or command keyword as well.
@ -44,6 +44,8 @@ This is useful on a 256-colour enabled xterm, where e.g. `colours="\033[38;5;22
**command** is command to be executed when regexp matches. Its output will be mixed with normal stdout, use redirectors (`>/dev/null`) if you want to suppress it.
**concat** is the name of a file which the current line will be appended to when the regexp matches.
**skip** can be `skip=yes`, if that case the matched line is skipped (discarded from the output), or `skip=no`, when it is not skipped. Default (if you do not have skip keyword) is of course not skipped.
**replace** means the regular expression match will be replaced by the value. All the preceeding regular expressions will be evaluated against the original text, but if they match and the replacement changes the length of the text, the colouring will *stay at the same positions*, which is probably not what you want - therefore put the `replace` rule preferrably at the beginning of config file.

7
grcat
View File

@ -130,7 +130,7 @@ while not is_last:
keyword = lower(keyword)
if keyword in ('colors', 'colour', 'color'):
keyword = 'colours'
if not keyword in ["regexp", "colours", "count", "command", "skip", "replace"]:
if not keyword in ["regexp", "colours", "count", "command", "skip", "replace", "concat"]:
raise ValueError("Invalid keyword")
ll[keyword] = value
@ -210,6 +210,11 @@ while 1:
else:
prevcount = "once"
pos = len(line)
if 'concat' in pattern:
with open(pattern['concat'], 'a') as f :
f.write(line + '\n')
if 'colours' not in pattern:
break
if 'command' in pattern:
os.system(pattern['command'])
if 'colours' not in pattern:

View File

@ -25,6 +25,7 @@ where keyword is one of:
.BR regexp ",
.BR colours ",
.BR command ",
.BR concat ",
.BR skip ",
.BR count ".
Only