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,
分配给变量。
解决方案
你可以使用这个sed
:
out=$(sed -n 's/.*TCP_IN = "\([^"]*\)".*/\1/p' file)
echo "$out"
20,21,22,25,53,80,110,143,443,465,587,