在记事本++中连续搜索两行
Search two lines continuously in notepad++
我有一个这样的文件
number 1
number 2
number 3
number 4
number 5
number 6
number 7
number 8
number 9
number 9
我想连续导出或搜索所有两行,我希望的结果是
number 4
number 5
number 6
number 7
我使用 notepad++ 或 bash linux 命令。
这是一个挑战,因为一切都设计为一次在一行上工作。此外,您还必须处理重叠的字符串以及开始和结束 fencepost 的情况。我不知道你的文件有多大,所以一次把所有东西都塞进去然后把它当作一个大字符串使用可能不可行。但是对于小数据集,使用合适的最新版本的 perl,您可以尝试:
perl -n0e 'while ( /(?=((^|\n) *\n(.+\n.+\n)( *\n|$)))/gi ) { print "\n"; }' datafile
基本上,匹配:
"(^|\n)" Beginning of file (string) or prior line's newline.
" *\n" A blank line.
"(.+\n" First line with data, paren starts data we will extract.
".+\n)" Second line with data, paren ends data we will extract.
"( *\n|$)" A blank line or end of file (string).
/(?=())/gi Matches overlapping strings, case ignored, all strings.
-e Following argument is a one-line program.
-n Assume while(<>) around program.
-0 Slurp entire argument (datafile) as single string.
对于较大的文件,您最好使用有限状态机。这不是特别漂亮。自从我在 awk 中工作以来已经有一段时间了。但这就足够了:
awk '
function printLines() { if ( i == 2 ) { print line[0]"\n"line[1]"\n"; } }
function zeroLines() { i=0; line[0]=""; line[1]=""; }
BEGIN { zeroLines() }
/^ *$/ { printLines(); zeroLines(); } # Matches blank line
/^ *./ { if ( i < 2 ) { line[i] = [=12=]; } i++; } # Matches non-blank line
END { printLines() }
' < datafile
我有一个这样的文件
number 1
number 2
number 3
number 4
number 5
number 6
number 7
number 8
number 9
number 9
我想连续导出或搜索所有两行,我希望的结果是
number 4
number 5
number 6
number 7
我使用 notepad++ 或 bash linux 命令。
这是一个挑战,因为一切都设计为一次在一行上工作。此外,您还必须处理重叠的字符串以及开始和结束 fencepost 的情况。我不知道你的文件有多大,所以一次把所有东西都塞进去然后把它当作一个大字符串使用可能不可行。但是对于小数据集,使用合适的最新版本的 perl,您可以尝试:
perl -n0e 'while ( /(?=((^|\n) *\n(.+\n.+\n)( *\n|$)))/gi ) { print "\n"; }' datafile
基本上,匹配:
"(^|\n)" Beginning of file (string) or prior line's newline.
" *\n" A blank line.
"(.+\n" First line with data, paren starts data we will extract.
".+\n)" Second line with data, paren ends data we will extract.
"( *\n|$)" A blank line or end of file (string).
/(?=())/gi Matches overlapping strings, case ignored, all strings.
-e Following argument is a one-line program.
-n Assume while(<>) around program.
-0 Slurp entire argument (datafile) as single string.
对于较大的文件,您最好使用有限状态机。这不是特别漂亮。自从我在 awk 中工作以来已经有一段时间了。但这就足够了:
awk '
function printLines() { if ( i == 2 ) { print line[0]"\n"line[1]"\n"; } }
function zeroLines() { i=0; line[0]=""; line[1]=""; }
BEGIN { zeroLines() }
/^ *$/ { printLines(); zeroLines(); } # Matches blank line
/^ *./ { if ( i < 2 ) { line[i] = [=12=]; } i++; } # Matches non-blank line
END { printLines() }
' < datafile