从多行文件获取子字符串

时间:2018-09-05 10:25:53

标签: bash

我有一个包含此内容的文件:

# Disabling this option will break firewall functionality that relies on
# stateful packet inspection (e.g. DNAT, PACKET_FILTER) and makes the firewall
# less secure
#
# This option should be set to "1" in all other circumstances
LF_SPI = "1"

# Allow incoming TCP ports
TCP_IN = "20,21,22,25,53,80,110,143,443,465,587,"

# Allow outgoing TCP ports
TCP_OUT = "20,21,22,25,53,80,110,113,143,443,465,587,"

# Allow incoming UDP ports
UDP_IN = "20,21,53,111"

# Allow outgoing UDP ports
# To allow outgoing traceroute add 33434:33523 to this list 
UDP_OUT = "20,21,53,113,123,111"

我需要在该行的结尾处获取TCP_IN = ","之间的文本。

我尝试了ALLOWEDPORTS=$(sed -e 's/.*TCP_IN = \"\(.*\)TCP_OUT.*/\1/' csf.conf),但正在获取整个文件。

我也尝试过ALLOWEDPORTS=$(sed -e 's/TCP_IN = \"\(.*\)TCP_OUT/\1/' csf.conf),但我也得到了整个文件。

有帮助吗?我需要将20,21,22,25,53,80,110,143,443,465,587,分配给该变量。

2 个答案:

答案 0 :(得分:0)

您可以使用此sed

out=$(sed -n 's/.*TCP_IN = "\([^"]*\)".*/\1/p' file)

echo "$out"

20,21,22,25,53,80,110,143,443,465,587,

答案 1 :(得分:0)

使用GNU grep:

$ inports=$(grep -Po 'TCP_IN = "\K[^"]*' infile)
$ declare -p inports
declare -- inports="20,21,22,25,53,80,110,143,443,465,587,"

-P启用与Perl兼容的正则表达式(\K所必需),而-o仅保留匹配项; \K是可变长度的正向后引号(“匹配所有内容,直到此处,然后将其丢弃”),[^"]*匹配除双引号之外的其他字符。


顺便说一句,使用命令获取整个文件的原因是,默认情况下,sed会打印每一行,即使该行未对它们进行任何操作。要抑制这种行为,请像在anubhava's answer中一样使用-n选项。