使用 sed 获取两个单词之间的文本
Get a text between two words using sed
我想找到两个词之间的文本,这两个词不在同一行,都出现在不同的行上,所以我想找到是的行(行的文本)介于单词之间
例如:
example-a, pqr- 411 037. ] .. abc.
V/s.
xyz abc Transports Ltd., ]
517, E, M. G. road, ]
hhhh. ] ..pqr.
I am doing the testing for example:
example.com
现在我想要一个在 V/s.
之后到 ...pqr
下一行的文本,而 ..pqr
下一行是空行。
我用了sed -nr '/v[/s]\.*/I{ :loop; n; /\.\.pqr/Iq; p; b loop}' input_file.txt
但是它给出的文本直到 hhhh. ] ..pqr
但我也想要下一行我怎么能用 sed 命令实现这个?
我觉得
sed -nr '/V\/s\.*/I { :loop; n; p; /\.\.pqr/I { :loop2; n; p; // b loop2; /^\s*$/ q }; b loop}' foo.txt
是最直接的改编。
即:
/^V\/s/I { # starting in a line that starts with v/s
# (you can go back to your old pattern if my guess about the
# precise criteria you want here is incorrect)
:loop
n # fetch the next line
p # print it
/\.\.pqr/I { # if it contained ..pqr
:loop2
n # fetch
p # and print lines
// b loop2 # until one does not contain ..pqr (// repeats the previous regex)
# this inner loop is necessary in case there are two lines
# containing ..pqr at the end of a section.
/^\s*$/ q # and if that line was empty, quit.
}
b loop # loop until then
}
我一开始更改了模式,因为在我看来 v[/s]\.*
是猜测的结果,直到正确的事情发生在示例文件中。 v[/s]\.*
将匹配 v/.
、vs.
、v/...
和 vs...
,但不会匹配 v/s.
-- []
表示一个字符集任何人都可以从中匹配,而不是序列 - [/s]
匹配 /
或 s
.
我输入的模式将匹配行首的 v/s
。或者(取决于您的需要),您可以使用 /v\/s/I
,它会匹配行中的任何地方的 v/s
,或者 /^v\/s\.*$/
,它只会匹配完全由 v/s
后跟任意数量的句点。
请注意,所有这些都是猜测,因为我不确切地知道是什么唯一标识了您文件中某个部分的开头。
sed -n '\#V/s\.#,/\.\.pqr/ {
\#V/s\.# b
/\.\.pqr/ n
p
}' YourFile
打印 所有部分的所有行 从 V/s.
开始的行到另一行第一个 pqr...
之后的第一行。
我会像这样使用 awk
:
awk '/V\/s\./ {f=1} /\.\.pqr/ && f {a=1} !NF && a {f=a=0} f' file
V/s.
xyz abc Transports Ltd., ]
517, E, M. G. road, ]
hhhh. ] ..pqr.
这将从找到的 Vs.
打印到 ..pqr
,下一行为空。
工作原理:
awk '
/V\/s\./ {f=1} # If "Vs." is found set flag "f"
/\.\.pqr/ && f {a=1} # If "..pqe" is found and flag "f" is true, set flag "a"
!NF && a {f=a=0} # If line is blank and "a" is true clear all flags
f # If flag "f" is true print the line.
' file
我想找到两个词之间的文本,这两个词不在同一行,都出现在不同的行上,所以我想找到是的行(行的文本)介于单词之间
例如:
example-a, pqr- 411 037. ] .. abc.
V/s.
xyz abc Transports Ltd., ]
517, E, M. G. road, ]
hhhh. ] ..pqr.
I am doing the testing for example:
example.com
现在我想要一个在 V/s.
之后到 ...pqr
下一行的文本,而 ..pqr
下一行是空行。
我用了sed -nr '/v[/s]\.*/I{ :loop; n; /\.\.pqr/Iq; p; b loop}' input_file.txt
但是它给出的文本直到 hhhh. ] ..pqr
但我也想要下一行我怎么能用 sed 命令实现这个?
我觉得
sed -nr '/V\/s\.*/I { :loop; n; p; /\.\.pqr/I { :loop2; n; p; // b loop2; /^\s*$/ q }; b loop}' foo.txt
是最直接的改编。
即:
/^V\/s/I { # starting in a line that starts with v/s
# (you can go back to your old pattern if my guess about the
# precise criteria you want here is incorrect)
:loop
n # fetch the next line
p # print it
/\.\.pqr/I { # if it contained ..pqr
:loop2
n # fetch
p # and print lines
// b loop2 # until one does not contain ..pqr (// repeats the previous regex)
# this inner loop is necessary in case there are two lines
# containing ..pqr at the end of a section.
/^\s*$/ q # and if that line was empty, quit.
}
b loop # loop until then
}
我一开始更改了模式,因为在我看来 v[/s]\.*
是猜测的结果,直到正确的事情发生在示例文件中。 v[/s]\.*
将匹配 v/.
、vs.
、v/...
和 vs...
,但不会匹配 v/s.
-- []
表示一个字符集任何人都可以从中匹配,而不是序列 - [/s]
匹配 /
或 s
.
我输入的模式将匹配行首的 v/s
。或者(取决于您的需要),您可以使用 /v\/s/I
,它会匹配行中的任何地方的 v/s
,或者 /^v\/s\.*$/
,它只会匹配完全由 v/s
后跟任意数量的句点。
请注意,所有这些都是猜测,因为我不确切地知道是什么唯一标识了您文件中某个部分的开头。
sed -n '\#V/s\.#,/\.\.pqr/ {
\#V/s\.# b
/\.\.pqr/ n
p
}' YourFile
打印 所有部分的所有行 从 V/s.
开始的行到另一行第一个 pqr...
之后的第一行。
我会像这样使用 awk
:
awk '/V\/s\./ {f=1} /\.\.pqr/ && f {a=1} !NF && a {f=a=0} f' file
V/s.
xyz abc Transports Ltd., ]
517, E, M. G. road, ]
hhhh. ] ..pqr.
这将从找到的 Vs.
打印到 ..pqr
,下一行为空。
工作原理:
awk '
/V\/s\./ {f=1} # If "Vs." is found set flag "f"
/\.\.pqr/ && f {a=1} # If "..pqe" is found and flag "f" is true, set flag "a"
!NF && a {f=a=0} # If line is blank and "a" is true clear all flags
f # If flag "f" is true print the line.
' file