English 中文(简体)
壳牌:在一只空白线之前获得所有线的简单途径
原标题:Shell: simple way to get all lines before first blank line
  • 时间:2009-10-21 20:26:37
  •  标签:

在你遇到第一个空白线之前,产生档案线的最坏指挥是什么? 例如:

output these
lines

but do not output anything after the above blank line
(or the blank line itself)

awk? 还有一点?

最佳回答
sed -e  /^$/,$d  <<EOF
this is text
so is this

but not this
or this
EOF
问题回答

http://www.un.org/Depts/DGACM/index_spanish.htm

awk -v  RS=

   1;{exit} 

sed -n -e  /./p;/./!q 
sed -e  /./!{d;q} 
sed -e  /./!Q    # thanks to Jefromi

如何直接造成伤亡?

while read line; do [ -z "$line" ] && break; echo "$line"; done

(Or printf %s instead of echo, if your shell is buggy and always handles escapes.)

改为:

sed  /^$/Q  <file>

Edit: s是路上、路上更快。 See ephemient s response for the Worst edition.

为了做到这一点,你可以:

awk  {if ($0 == "") exit; else print}  <file>

请注意,我故意写这封信是为了避免使用定期表述。 我不知道内部优化是什么样子,但我怀疑直接细化比较会更快。

# awk  !NF{exit}1  file
output these
lines

紧急解决办法

awk  /^$/{exit} {print}   <filename>

这里使用Perl的解决办法:

#! perl

use strict;
use warnings;

while (<DATA>) {
    last if length == 1;
    print;
}

__DATA__
output these
lines

but don t output anything after the above blank line
(or the blank line itself)

A couple of Perl one-liners

$ perl -pe last if /^$/  file..

$ perl -lpe last unless length  file..

另一种解决办法:

perl -00 -ne  print;exit  file
perl -00 -pe  exit if $. == 2  file




相关问题
热门标签