繁体   English   中英

使用bash读取文本文件中的行的子集

[英]Reading a subset of the lines in a text file, with bash

我有一个文件

line a - this is line a
line b - this is line b
line c - this is line c
line d - this is line d
line e - this is line e

问题是:如何使用bash命令输出从“line b”开始直到“line d”的行? 我的意思是,获得:

"line b - this is line b
 line c - this is line c
 line d - this is line d"
sed -n '/line b/,/line d/p' file

您的示例不足以在一般情况下推断出您想要的内容,但假设您要删除第一行和最后一行,则可以使用

tail -n+2 $filename | head -n-1

这里tail -n+2打印从第二行开始的所有行, head -n-1打印除了last之外的所有行。

对于您的样本数据集:

awk '/line b/,/line d/' file

要么

awk '/line d/{f=0;print}/line b/{f=1}f' file

如果用bash,你的意思实际上是单独打击,我无法帮助你。 你真的应该使用正确的工具来完成工作。 如果你的意思是你可以从bash调用的标准UNIX实用程序,我将使用awk

echo 'line a - this is line a
line b - this is line b
line c - this is line c
line d - this is line d
line e - this is line e' | awk '
    BEGIN {e=0}
    /^line b/ {e=1}
    /^line d/ {if (e==1) {print;exit}}
    {if (e==1) print}
'

这输出:

line b - this is line b
line c - this is line c
line d - this is line d

它的工作方式很简单。

  • e是echo标志,最初设置为false(0)。
  • 当你找到第b行时,将echo设置为true(1) - 不要打印。 这将由下面的最后一个要点处理。
  • 当你找到d行并且回显打开时,打印并退出。
  • 当echo打开时,打印该行(包括行b)。

我在这里做了一个假设,除非你已经回应,否则你不想退出d行。 如果这是错误的,请将出口移到行d的if语句之外:

    /^line d/ {if (e==1) print;exit}

然后,如果你在你的第b行之前得到一行,它就会退出而不会回响任何东西。

"/^line X/" -type子句可以非常强大,可以很好地匹配你可以抛出的任何东西。

你可以单独使用bash来做,虽然我同意Pax使用其他工具可能是一个更好的解决方案。 这是一个仅限bash的解决方案:

while read line
do
    t=${line#line b}
    if test "$t" != "$line"
    then
        echo $line
        while read line
        do
            echo $line
            t=${line#line d}
            if test "$t" != "$line"
            then
                exit 0
            fi
        done
    fi
done

另一种方法取决于你的意思:

pcregrep -m 'line b - this is line b
 line c - this is line c
 line d - this is line d' file

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM