在文本文件中搜索字符串并打印行直到下一个选项卡\ t

Sha*_*ang 0 perl grep parsing string-matching

我有一个相当复杂的文本文件file1.txt,没有正确使用.然而,该文件以制表符分隔,即每个字符串由\t.分隔.

我想编写一个脚本/使用一个Unix命令解析整个文件中的某个字符串string1:,该字符串将在冒号后打印该行直到停止\t.

文本文件如下所示:

...kjdafhldkhlfak\tSTRING1:Iwanttokeepthis\tfadfasdafldafh\tSTRING1:andthis\tafsdkfasldh....
Run Code Online (Sandbox Code Playgroud)

所以grep类似的功能输出

Iwanttokeepthis
andthis
Run Code Online (Sandbox Code Playgroud)

在Perl中,我知道如何使用字符串打印字符串

perl -wln -e 'print if /\bSTRING1\b/' file1.txt
Run Code Online (Sandbox Code Playgroud)

如何将一个修订本打印之间的界限STRING1:\t

daw*_*awg 5

使用Perl:

$ echo $'kjdafhldkhlfak\tSTRING1:Iwanttokeepthis\tfadfasdafldafh\tSTRING1:andthis\tafsdkfasldh' > /tmp/file
perl -lne 'while (/STRING1:([^\t]+)\t/g) {print $1}' /tmp/file
Iwanttokeepthis
andthis
Run Code Online (Sandbox Code Playgroud)

或者,如评论中所述:

$ perl -nle'print for /STRING1:([^\t]*)\t/g' /tmp/file
Iwanttokeepthis
andthis
Run Code Online (Sandbox Code Playgroud)

  • 更简单:/ STRING1的`perl -nle'print :( [^\t]*)\ t/g'`,或者只是/ STRING1的`perl -nle'print :( [^\t]*)/ g' ` (2认同)