2011-01-08 20 views

回答

0
awk ' 
    BEGIN     { noprint = 1; cue = ARGV[1]; ARGV[1] = ""} 
    (!noprint)    { print } 
    (noprint && $0 == cue) { noprint = 0 } 
' house textfile 
+0

這一個不適合我的工作,我得到根本沒有輸出。 – 2011-01-08 14:52:30

+0

嘿,什麼是-1?這適用於Debian 5.0上的`gawk` 3.1.5。 – 2011-01-08 14:58:36

0

您可以用Perl的一行代碼,你當然可以納入一個bash腳本很容易做到這一點。

$ perl -ne 'print if $ok; $ok=1 if /^house$/' sample.txt 
cat 
dog 
etc 
6
sed -n '0,/^house$/d;p' input.txt 
0

您也可以使用Unix文本編輯器編輯這種類型的任務。請注意,ed會將整個文件讀入內存,並在原地進行文件編輯,而無需事先備份。

# prints result to stdout 
printf '%s\n' H ',g/^house$/1,/^house$/d' ',p' | ed -s file 
printf '%s\n' H ',g/^house$/1,//d' ',p' | ed -s file # short version 

# edits file in-place 
printf '%s\n' H ',g/^house$/1,/^house$/d' wq | ed -s file 

有關編輯的詳細信息,請參閱:

「從腳本編輯的文本編輯器編輯文件」,

http://wiki.bash-hackers.org/howto/edit-ed

相關問題