简体   繁体   English

将 awk 与 gedit 外部工具一起使用

[英]Using awk with gedit external tools

I have used gedit with Ubuntu a lot and now transitioning on Macos.我已经将gedit与 Ubuntu 一起使用了很多,现在在 Macos 上进行转换。 I noticed that some of the plugins in the Macos version are missing.我注意到 Macos 版本中的一些插件丢失了。 For instance, there is no (AFAIK) an out-of-the-box option to comment/uncomment code.例如,没有(AFAIK)评论/取消注释代码的开箱即用选项。 However, there is the possibility to define an external tool to basically have whatever you want.但是,可以定义一个外部工具来基本上拥有您想要的任何东西。

I want to just comment the text selection in R/python style (prepending a # before each line of the input).我只想以 R/python 样式评论文本选择(在输入的每一行之前添加一个# )。 I went to Tools -> Manage External Tools and defined a "Comment Code" tool in this way:我去了工具 - >管理外部工具并以这种方式定义了一个“评论代码”工具:

#!/bin/bash
awk '{print "#" $0}'

and set Input as "Current Selection" and output as "Replace Current Selection".并将输入设置为“电流选择”,将 output 设置为“替换电流选择”。

It works if you select a text;如果你 select 一个文本,它就可以工作; however if you don't select anything, it stalls forever, since (for what I understood) awk is waiting for an input.但是,如果您没有 select 任何东西,它就会永远停止,因为(据我所知) awk正在等待输入。

How can I avoid this problem?我怎样才能避免这个问题? Of course I don't need a awk (or whatever) solution, whatever works is fine.当然,我不需要awk (或其他)解决方案,任何工作都可以。 I'm not much expert of bash tools like awk or sed , so very likely I'm missing something very simple.我不是bash工具的专家,例如awksed ,所以很可能我错过了一些非常简单的东西。

See https://www.gnu.org/software/gawk/manual/html_node/Read-Timeout.html for how to implement a read timeout with GNU awk.请参阅https://www.gnu.org/software/gawk/manual/html_node/Read-Timeout.html了解如何使用 GNU Z5E4C8DFA9E20567E2655E847B66 实现读取超时。

With input from stdin (enter 7 then Enter/Return then Control-D):从 stdin 输入(输入 7 然后 Enter/Return 然后 Control-D):

$ gawk 'BEGIN{PROCINFO["-", "READ_TIMEOUT"]=5000} {print "#" $0}'
7
#7

or with input from a pipe:或来自 pipe 的输入:

$ seq 2 | gawk 'BEGIN{PROCINFO["-", "READ_TIMEOUT"]=5000} {print "#" $0}'
#1
#2

or from a file:或从文件中:

$ seq 2 > file
$ gawk 'BEGIN{PROCINFO["-", "READ_TIMEOUT"]=5000} {print "#" $0}' file
#1
#2

but if you don't provide any input and wait 5 seconds:但如果您不提供任何输入并等待 5 秒:

$ gawk 'BEGIN{PROCINFO["-", "READ_TIMEOUT"]=5000} {print "#" $0}'
gawk: cmd. line:1: fatal: error reading input file `-': Connection timed out
$

You can always redirect stderr the usual way if you don't want to see the timeout message:如果您不想看到超时消息,您可以随时以通常的方式重定向 stderr:

$ gawk 'BEGIN{PROCINFO["-", "READ_TIMEOUT"]=5000} {print "#" $0}' 2>/dev/null
$

but of course that would mask ALL error messages so you might want to do this instead to just mask that one message:但当然这会掩盖所有错误消息,因此您可能想要这样做而不是仅掩盖一条消息:

{ gawk 'BEGIN{PROCINFO["-", "READ_TIMEOUT"]=5000} {print "#" $0}' 2>&1 >&3 |
    grep -v 'fatal: error reading input file'; } 3>&1

eg:例如:

$ { gawk 'BEGIN{print "other error" >"/dev/stderr";
        PROCINFO["-", "READ_TIMEOUT"]=5000} {print "#" $0}' 2>&1 >&3 |
    grep -v 'fatal: error reading input file'; } 3>&1
other error
$

Obviously change the 5000 to however many milliseconds you want the script to wait for input.显然将5000更改为您希望脚本等待输入的毫秒数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM