Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Linux上为不同元素之间的输出调整日志_Linux_Grep - Fatal编程技术网

在Linux上为不同元素之间的输出调整日志

在Linux上为不同元素之间的输出调整日志,linux,grep,Linux,Grep,我如何反复搜索一个文件,从一段文本开始,到另一段文本结束? 例如,我有一个非常大的日志。日志中有以特定文本开头和结尾的部分,例如: [startset] 废话废话 [/startset] 我只想收集[startset]和[startset]之间的文本。本节将再次出现,因此我需要循环并继续执行此操作,直到所有文本都已收集(并转储到单独的文件中) 谢谢我不能用grep做这件事。你对perl脚本还满意吗?这对我来说是最简单的。假设您的文件坚持这种格式,下面是一个基本的脚本,几乎没有错误检查 我的数据

我如何反复搜索一个文件,从一段文本开始,到另一段文本结束?
例如,我有一个非常大的日志。日志中有以特定文本开头和结尾的部分,例如:
[startset] 废话废话 [/startset]
我只想收集[startset]和[startset]之间的文本。本节将再次出现,因此我需要循环并继续执行此操作,直到所有文本都已收集(并转储到单独的文件中)


谢谢

我不能用grep做这件事。你对perl脚本还满意吗?这对我来说是最简单的。假设您的文件坚持这种格式,下面是一个基本的脚本,几乎没有错误检查

我的数据文件

testlinux:~ # cat sample.txt
ignore
ignore
[startset]
grab this 1
grab this 2
[/startset]
ignore
ignore
[startset]
grab this 3
grab this 4
[/startset]
ignore this
ignore that
testlinux:~ #
testlinux:~ # ./sample.pl
grab this 1
grab this 2
grab this 3
grab this 4
testlinux:~ #
我的perl脚本

testlinux:~ # cat sample.pl
#!/usr/bin/perl

$infile="sample.txt";

open(INFILE,"$infile");

$startset=0;

while (<INFILE>) {
    chop($line=$_);
    if ($line =~ /\[startset\]/) {
            $startset=1;
            chop($line=<INFILE>);
    }
    if ($line =~ /\[\/startset\]/) {
            $startset=0;
    }
    if ($startset eq 1) {
            print "$line\n";
    }
}


testlinux:~ # 

您可以使用
awk
。这将使用来自Bens post的数据:

awk '/\[\/startset\]/ {f=0} f; /\[startset\]/ {f=1}' file
grab this 1
grab this 2
grab this 3
grab this 4
大体上

awk '/stop/ {f=0} f; /start/ {f=1}'
如果满足条件,则使用标志
f
控制输出

awk '/stop/ {f=0} f; /start/ {f=1}'