我有一个如下所示的文件:
odule(load="imfile" PollingInterval="10")
$MaxMessageSize 65536
#RSYSLOG_ForwardFormat template is used to enable millisecond-accuracy
$ActionForwardDefaultTemplate RSYSLOG_ForwardFormat
module(load="imudp")
input(type="imudp" port="5140")
mail.info stop
# This is rule for filtering apache logs
:msg, regex, ".*DHCP.*" stop
:msg, regex, ".*HTTP/1.1\" 200 [0-9-]* \"-\" \"ClusterListener/1.0\".*" stop
global(
workDirectory="/var/lib/rsyslog" # Directory where queue files will be stored
)
Run Code Online (Sandbox Code Playgroud)
我想提取 的值port(即 5140)并将其存储在环境变量中。
我尝试过使用 awk、grep 和 cat 命令的变体来尝试实现这一目标,但似乎没有一个给出我想要的输出。
例如,sed -n 's/port=\(.*\)/\1/p' < file.conf给出以下内容:
input(type="imudp" "5140")
Run Code Online (Sandbox Code Playgroud)
这不是我想要的。我只是想存储5140到环境变量中。
有人能帮忙吗?
既然您标记了它,并且我们已经有了一种sed方法grep,那么这是 awk 方法;)
awk -F'[ "]' '/port=/{ print $5 }' file.conf
Run Code Online (Sandbox Code Playgroud)
或者将其放在变量中myvar:
myvar=$(awk -F'[ "]' '/port=/{ print $5 }' file.conf)
Run Code Online (Sandbox Code Playgroud)