【问题标题】:Print lines in file from the match line until end of file从匹配行到文件末尾打印文件中的行
【发布时间】:2011-03-26 22:56:04
【问题描述】:

我编写了以下 awk 来打印从匹配行到 EOF 的行

awk '/match_line/,/*/' file

如何在 sed 中做同样的事情?

【问题讨论】:

    标签: sed


    【解决方案1】:

    这是针对 Windows 上真正旧版本的 GNU sed

    GNU sed 2.05 版

    http://www.gnu.org/software/sed/manual/sed.html

    -n only display if Printed
    -e expression to evaluate
    p stands for Print
    $ end of file
    line1,line2 is the range
    ! is NOT
    

    干草堆.txt

    abc
    def
    ghi
    needle
    want 1
    want 2
    

    将匹配的行和后面的行打印到文件末尾

    >sed.exe -n -e "/needle/,$p" haystack.txt
    needle
    want 1
    want 2
    

    打印文件的开头直到但不包括匹配行

    >sed.exe -n -e "/needle/,$!p" haystack.txt
    abc
    def
    ghi
    

    打印文件开头直到 AND 包括匹配行

    >sed.exe -n -e "1,/needle/p" haystack.txt
    abc
    def
    ghi
    needle
    

    打印匹配行之后的所有内容

    >sed.exe -n -e "1,/needle/!p" haystack.txt
    want 1
    want 2
    

    打印两个匹配项之间的所有内容 - 包括

    >sed.exe -n -e "/def/,/want 1/p" haystack.txt
    def
    ghi
    needle
    want 1
    

    【讨论】:

    • 谢谢:比检查的解释更好。 FWIW,另一个例子(不幸不会格式化多:-( # show global/file attributes of some compressed netCDF files 987654330 FN='emis_mole_all_2008*' 987654332 echo -e "Processing ${GZ}, start=$(date)" 987654334 NETCDF_FN="${GZ_FN%.gz}" 987654336 gunzip ./${GZ_FN} 987654338 echo # newline between files 987654340 @
    • 如果你想在'def'和'want 1'之间打印怎么办?
    • @ikwyl6 - 我在最后添加了另一个示例来回答您的问题。
    【解决方案2】:
    sed -n '/matched/,$p' file
    awk '/matched/,0' file
    

    【讨论】:

    • 这里的$是指文件的结尾,而不是行的结尾吗?
    • @FelipeAlvarez 是的 $ 在 sed 中表示 end of file
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    相关资源
    最近更新 更多