Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 获取带有特定单词的行后的字符串_String_Awk_Sed_Get_Next - Fatal编程技术网

String 获取带有特定单词的行后的字符串

String 获取带有特定单词的行后的字符串,string,awk,sed,get,next,String,Awk,Sed,Get,Next,对于openwrt,我通常使用UCI获取一些脚本变量中的数据。诸如此类: uci get /etc/config/network.lan.data 不幸的是,我没有发现与Linux大体类似的东西,但我相信awk或sed可以做到这一点,而不需要大量的输出 通常,第一个字符串只是查找第二个字符串的参数 输入: ... config 'wan' data 'a1 a2 a3' something 'words' config 'lan' info 'words'

对于openwrt,我通常使用UCI获取一些脚本变量中的数据。诸如此类:

uci get /etc/config/network.lan.data
不幸的是,我没有发现与Linux大体类似的东西,但我相信awk或sed可以做到这一点,而不需要大量的输出

通常,第一个字符串只是查找第二个字符串的参数

输入:

    ...

config 'wan'
    data 'a1 a2 a3'
    something 'words'

config 'lan'
    info 'words'
    data 'b1 b2 b3'
    something 'words'

config 'net'
    something 'words'
    info 'words'
    data 'c1 c2'

    ...
    ...

config something 'wan'
    something some_data 'a1 a2 a3'
    something 'words'

config something 'lan'
    something some_info 'words'
    something some_data 'b1 b2 b3'
    something 'words'

config something 'net'
    something 'words'
    something some_info 'words'
    something some_data 'c1 c2'

    ...
输出:

b1 b2 b3
b1 b2 b3
b1 b2 b3
b1 b2 b3
b1 b2 b3
---编辑:--

我相信,通过此表单的输入,脚本的功能将更加广泛:

输入:

    ...

config 'wan'
    data 'a1 a2 a3'
    something 'words'

config 'lan'
    info 'words'
    data 'b1 b2 b3'
    something 'words'

config 'net'
    something 'words'
    info 'words'
    data 'c1 c2'

    ...
    ...

config something 'wan'
    something some_data 'a1 a2 a3'
    something 'words'

config something 'lan'
    something some_info 'words'
    something some_data 'b1 b2 b3'
    something 'words'

config something 'net'
    something 'words'
    something some_info 'words'
    something some_data 'c1 c2'

    ...
请注意请求,这里有一个更好的解释:

1-查找带有“lan”的行(或者可能带有config.*.lan)

2-如果找到,请在以下行中搜索带有单词结尾数据(可能是*.data)的第一行

3-打印此行“”之间的内容

输出:

b1 b2 b3
b1 b2 b3
b1 b2 b3
b1 b2 b3
b1 b2 b3
最好的解决方案是什么


感谢您的关注

您可以使用以下内容:

awk -v C="lan" -v F="data" '$1=="config" { gsub(/^[\47"]|[\47"]$/,"",$2); conf=$2; next } conf==C && $1==F { $1=""; gsub(/^ *[\47"]|[\47"]$/, ""); print }' YOURFILE
输入:

config 'wan'
    data 'a1 a2 a3'
    something 'words'

config 'lan'
    info 'words'
    data 'b1 b2 b3'
    something 'words'

config 'net'
    something 'words'
    info 'words'
    data 'c1 c2'    
config something 'wan'
    something some_data 'a1 a2 a3'
    something 'words'

config something 'lan'
    something some_info 'words'
    something some_data 'b1 b2 b3'
    something 'words'

config something 'net'
    something 'words'
    something some_info 'words'
    something some_data 'c1 c2'
输出:

b1 b2 b3
b1 b2 b3
b1 b2 b3
b1 b2 b3
b1 b2 b3

更新问题的处理方法略有不同:

awk -v C="lan" -v F="data" 'BEGIN { FS="\47"; REG=".*"F"[ \t]*" } $1~"config[ \t]" { conf=$2 } conf==C && $1~REG { print $2 }' YOURFILE
输入:

config 'wan'
    data 'a1 a2 a3'
    something 'words'

config 'lan'
    info 'words'
    data 'b1 b2 b3'
    something 'words'

config 'net'
    something 'words'
    info 'words'
    data 'c1 c2'    
config something 'wan'
    something some_data 'a1 a2 a3'
    something 'words'

config something 'lan'
    something some_info 'words'
    something some_data 'b1 b2 b3'
    something 'words'

config something 'net'
    something 'words'
    something some_info 'words'
    something some_data 'c1 c2'
输出:

b1 b2 b3
b1 b2 b3
b1 b2 b3
b1 b2 b3
b1 b2 b3

您可以使用以下内容:

awk -v C="lan" -v F="data" '$1=="config" { gsub(/^[\47"]|[\47"]$/,"",$2); conf=$2; next } conf==C && $1==F { $1=""; gsub(/^ *[\47"]|[\47"]$/, ""); print }' YOURFILE
输入:

config 'wan'
    data 'a1 a2 a3'
    something 'words'

config 'lan'
    info 'words'
    data 'b1 b2 b3'
    something 'words'

config 'net'
    something 'words'
    info 'words'
    data 'c1 c2'    
config something 'wan'
    something some_data 'a1 a2 a3'
    something 'words'

config something 'lan'
    something some_info 'words'
    something some_data 'b1 b2 b3'
    something 'words'

config something 'net'
    something 'words'
    something some_info 'words'
    something some_data 'c1 c2'
输出:

b1 b2 b3
b1 b2 b3
b1 b2 b3
b1 b2 b3
b1 b2 b3

更新问题的处理方法略有不同:

awk -v C="lan" -v F="data" 'BEGIN { FS="\47"; REG=".*"F"[ \t]*" } $1~"config[ \t]" { conf=$2 } conf==C && $1~REG { print $2 }' YOURFILE
输入:

config 'wan'
    data 'a1 a2 a3'
    something 'words'

config 'lan'
    info 'words'
    data 'b1 b2 b3'
    something 'words'

config 'net'
    something 'words'
    info 'words'
    data 'c1 c2'    
config something 'wan'
    something some_data 'a1 a2 a3'
    something 'words'

config something 'lan'
    something some_info 'words'
    something some_data 'b1 b2 b3'
    something 'words'

config something 'net'
    something 'words'
    something some_info 'words'
    something some_data 'c1 c2'
输出:

b1 b2 b3
b1 b2 b3
b1 b2 b3
b1 b2 b3
b1 b2 b3
$awk'
开始{
RS=”“#空RS以空行分隔记录
FS=“*\47[\n]*”#FS是一个单引号,周围有空格和\n
}
匹配($0,“config”FS“lan”){#如果记录有正确的配置行
对于(i=1;i
$awk'
开始{
RS=”“#空RS以空行分隔记录
FS=“*\47[\n]*”#FS是一个单引号,周围有空格和\n
}
匹配($0,“config”FS“lan”){#如果记录有正确的配置行
对于(i=1;iWith),您可以将
RS
设置为空字符串,将每一行设置为字段,将
FS
设置为新行,从而将每一节视为一条记录

解析.awk

开始{
RS=“”
FS=“\n”
q=“”#引号字符的方便定义
}
$1~QCQ{
对于(i=2;iWith),您可以将
RS
设置为空字符串,将每一行设置为字段,将
FS
设置为新行,从而将每一节视为一条记录

解析.awk

开始{
RS=“”
FS=“\n”
q=“”#引号字符的方便定义
}
$1~QCQ{
对于(i=2;i和gnu sed

猫莎草

:A
/^config/!d
/lan/!d
:B
N
s/.*\n//
/^config/bA
/.*data[^']*/!bB
s///
:C
s/([^']*)(')([^']*.*)/\1\3/
tC
p
bB
你这样叫它

sed -nEf sedscript infile
使用gnu sed

猫莎草

:A
/^config/!d
/lan/!d
:B
N
s/.*\n//
/^config/bA
/.*data[^']*/!bB
s///
:C
s/([^']*)(')([^']*.*)/\1\3/
tC
p
bB
你这样叫它

sed -nEf sedscript infile

不够清楚,你能告诉我从哪里在输入文件中执行搜索吗?请更清楚一点。如果你假设我们知道什么是
openwrt,我通常使用UCI
来说明你的要求,那么YMMV。告诉我们输出
b1 b2 b3
的标准是什么,因为有几种选择和可能实施解决方案的可行方法,(第二次出现
数据
?包含
lan
?其他内容的块?),因此,到目前为止,您可以选择折衷的答案!1-搜索包含“lan”(或“Something.*.lan”)的第一行;2-如果在包含“date”的第一行下方找到它并打印“”之间的值不够清晰,请务必让我知道从哪里在输入文件中执行搜索?请对此更加清楚。如果您假设我们知道什么是
openwrt,我通常使用UCI
来说明您的要求,那么YMMV。告诉我们输出
b1 b2 b3
的标准是什么,因为有服务器所有备选方案和几种可能的实施解决方案的方法,(第二次出现
数据
?包含
lan
?其他内容的块?),因此,到目前为止,您对答案的选择是折衷的!1-搜索包含“lan”(或“Something.*.lan”)的第一行;2-如果在包含“date”的第一行下方找到它并打印“”之间的值,但它会对数据进行更多假设,如用空行分隔的记录,字段值必须以单引号开头,只有一个字段值(会删除数据“b1 b2 b3”“b4 b5”的一些数据)
).@AndriyMakukha您在解决方案中准备了
数据'b1 b2 b3''b4 b5'
吗?没有,但由于我的解决方案只输出字段名后面的所有内容,只从两边删除引号,它将输出
b1 b2 b3''b4 b5
,这更安全。您说的更多单词是什么意思。请为该请求发布匹配的数据。我修改了您的cod使用.*in###($0,config.*FS“lan”)###,并为输入工作:配置某些“lan”#####但我不能使用###“if($i==“*.data”)###:某些数据“b1 b2 b3”很漂亮,但它对数据进行了更多假设,如用空行分隔的记录,字段值必须以单引号开头,只有一个字段值(会删除数据“b1 b2 b3”“b4 b5”“的一些数据)
).@AndriyMakukha您在解决方案中准备了
数据'b1 b2 b3''b4 b5'
吗?没有,但由于我的解决方案只输出字段名后面的所有内容,只从两边删除引号,它将输出
b1 b2 b3''b4 b5
,这更安全。您说的更多单词是什么意思。请为该请求发布匹配的数据。我修改了您的cod在###($0,config.*.FS“lan”)###中使用.*in##($0,config.*FS“lan”)##########:一些#数据'b1 b2 b3'