Ruby on rails 4 使用sed获取两个单词之间的文本
我想找到两个单词之间的文本,这两个单词不在同一行,都在不同的行上,所以我想找到两个单词之间的行(行中文本) 例如:Ruby on rails 4 使用sed获取两个单词之间的文本,ruby-on-rails-4,sed,Ruby On Rails 4,Sed,我想找到两个单词之间的文本,这两个单词不在同一行,都在不同的行上,所以我想找到两个单词之间的行(行中文本) 例如: example-a, pqr- 411 037. ] .. abc. V/s. xyz abc Transports Ltd., ] 517, E, M. G. road, ] hhhh. ] ..pqr. I am doing the te
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}输入文件.txt
但是它给文本直到hhh.]。。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.
,[/code>表示任何人都可以匹配的字符集,而不是序列--[/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