在“>和 pattern中提取字符串

时间:2022-06-02 00:19:26

I am trying to extract the string from a file having following pattern within a line

我试图从一行中具有以下模式的文件中提取字符串

>------ </

The ----- represents can be any variable length string. The start pattern within a line is > and end pattern </.

-----表示可以是任何可变长度的字符串。一行中的起始模式是>和结束模式

Using regex of VIM is command line search possible? and if so could that be printed?

使用VIM的正则表达式可以进行命令行搜索吗?如果是这样可以打印出来?

Or will have to write a script?

或者必须写一个脚本?

I am a new user to VIM

我是VIM的新用户

2 个解决方案

#1


Try this

%:s/.*>\(.*\)<\/.*/\1/    

#2


Try the following vim search:

尝试以下vim搜索:

/">\(.*\)<\/

That should match any line with that pattern. It'll also store whatever text it grabs in between your start and end markers into \1 which you can use if you want to do search and replace in vim. For example:

这应该与该模式的任何行匹配。它还会将你在开始和结束标记之间抓取的任何文本存储到\ 1中,如果你想在vim中进行搜索和替换,你可以使用它。例如:

:%s/">\(.*\)<\//Log message: \1/

If you want to use grep in the command line to search for that string you can use:

如果要在命令行中使用grep来搜索该字符串,可以使用:

$ egrep "\">.*<\/" foo.txt

This will print out only the matching lines from foo.txt. If you want to send these to a new file try:

这将只打印出来自foo.txt的匹配行。如果要将这些文件发送到新文件,请尝试:

$ egrep "\">.*<\/" foo.txt > new.txt

#1


Try this

%:s/.*>\(.*\)<\/.*/\1/    

#2


Try the following vim search:

尝试以下vim搜索:

/">\(.*\)<\/

That should match any line with that pattern. It'll also store whatever text it grabs in between your start and end markers into \1 which you can use if you want to do search and replace in vim. For example:

这应该与该模式的任何行匹配。它还会将你在开始和结束标记之间抓取的任何文本存储到\ 1中,如果你想在vim中进行搜索和替换,你可以使用它。例如:

:%s/">\(.*\)<\//Log message: \1/

If you want to use grep in the command line to search for that string you can use:

如果要在命令行中使用grep来搜索该字符串,可以使用:

$ egrep "\">.*<\/" foo.txt

This will print out only the matching lines from foo.txt. If you want to send these to a new file try:

这将只打印出来自foo.txt的匹配行。如果要将这些文件发送到新文件,请尝试:

$ egrep "\">.*<\/" foo.txt > new.txt