簡體   English   中英

從文本文件的第一個空白行開始刪除所有內容

[英]Remove everything starting from the first blank line in a text file

我有一個文本文件,我想從中刪除所有從第一個空白行開始的行,最好使用一些標准的Unix工具,如sed / awk。

使用Perl

perl -pe '/^\s+$/ and exit' < in.txt > out.txt

-p在命令行中使用-e加上while(<>) {print }

這應該工作:

awk '/^$/ {exit}{print}' < data

輸入樣例:

 one two thre four five six seven eight nine ten eleven twelve thirteen fourteen fifteen THE END 

樣本輸出:

 # awk '/^$/ {exit}{print}' < data one two thre four five six seven 

順便說一句,如果您的“空白行”可能包含空格和/或制表符,請使用:

'/^[ \t]*$/ {exit}{print}'

與sed:

$ sed -i '/^\s*$/,$d' file

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM