我有很多的話就像一個文件:bash腳本刪除文本文件中的所有行,直到一個字符串
one
two
three
four
tree
house
cat
dog
etc
,我要刪除所有字,直到「家」以這樣的方式來獲得只有:
cat
dog
etc
我有很多的話就像一個文件:bash腳本刪除文本文件中的所有行,直到一個字符串
one
two
three
four
tree
house
cat
dog
etc
,我要刪除所有字,直到「家」以這樣的方式來獲得只有:
cat
dog
etc
awk '
BEGIN { noprint = 1; cue = ARGV[1]; ARGV[1] = ""}
(!noprint) { print }
(noprint && $0 == cue) { noprint = 0 }
' house textfile
您可以用Perl的一行代碼,你當然可以納入一個bash腳本很容易做到這一點。
$ perl -ne 'print if $ok; $ok=1 if /^house$/' sample.txt
cat
dog
etc
sed -n '0,/^house$/d;p' input.txt
您也可以使用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
有關編輯的詳細信息,請參閱:
「從腳本編輯的文本編輯器編輯文件」,
這一個不適合我的工作,我得到根本沒有輸出。 – 2011-01-08 14:52:30
嘿,什麼是-1?這適用於Debian 5.0上的`gawk` 3.1.5。 – 2011-01-08 14:58:36