Match if previous line starts with character

十年热恋 提交于 2019-12-06 10:10:54

问题


I have a file, that looks like this:

expression1
- expresson1.1
- expressoion1.2
expression2
-expression2.1
expression3
-expression3.1
-expression3.2
-expression3.3

What I want to do is, delete lines 1.2, 3.2 and 3.3, so only lines that do not start with a minus, and the next line remain.

So I tried writing a regex that matches every line starting with a minus where the previous one also started with a minus, and then delete them.

So far, no success. Any hints would be very appreciated.


回答1:


if an awk solution would be accepted by you, check the one-liner below:

awk '/^[^-]/{f=1;print;next;}{if(f && $0~/^-/){print;f=0;}}' yourFile

test

kent$  echo "expression1
dquote> - expresson1.1
dquote> - expressoion1.2
dquote> expression2
dquote> -expression2.1
dquote> expression3
dquote> -expression3.1
dquote> -expression3.2
dquote> -expression3.3
dquote> "|awk '/^[^-]/{f=1;print;next;}{if(f && $0~/^-/){print;f=0;}}'
expression1
- expresson1.1
expression2
-expression2.1
expression3
-expression3.1



回答2:


You can use this regex:

/(-.+)\s((-.+)?(\s|))+/g

Replacing the matches with:

$1\n

You can see this regex in action with RegExr here.

P.S. tough problem, here lookarounds don't work because you don't have fixed length prefixes to match against, while lookbehind (and lookahead) require fixed length strings as pointed out here (near the end of the page).




回答3:


Depending on your regex flavor there is probably a way to activate multiline mode. In this case, ^ and $ match line beginnings and endings in addition to the start and end of the whole string.

/^-.*$\n((^-.*$\n?)+)/

This should match at least two consecutive lines starting with -. The first capture/subpattern contains all lines after the first one. So these matches would be what you want to delete.

Of course, replace \n with the line ending appropriate for your system.



来源:https://stackoverflow.com/questions/12598414/match-if-previous-line-starts-with-character

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!