[英]windows batch programming: Removing only exactly matching line from the file
我有一個像這樣的文件:
exit;
exit_cool
int exit =30
exitstatus;
exit ;
exit;
some exit;
coolexit;
this line should not be removed.
exitexit;
我要刪除僅包含“退出”的行。 因此我希望該命令僅從文件中刪除第一行。我目前正在使用此命令:
type somefile.txt | find /v "exit;"
但是我得到的輸出是:
exit_cool
int exit =30
exitstatus;
exit ;
this line should not be removed.
如您所見,該命令刪除了所有包含exit;
的行exit;
我只希望刪除第1行,因為它是完全匹配的。有幫助嗎?
FIND無法做您想要的。 但是FINDSTR可以:-)
將FINDSTR與正則表達式結合使用^
代表行的開頭, $
代表行的結尾:
findstr /v "^exit;$" "somefile.txt"
或使用具有完全相同含義的/x
選項,但也可以將其與字符串文字一起使用,而不僅僅是正則表達式:
findstr /v /x /c:"exit;" "somefile.txt"
請注意,僅當文本文件使用以回車符/換行符終止的Windows樣式行時, $
anchor和/x
選項(和/e
end選項)的行尾才能正常工作。 僅以換行符結尾的Unix樣式行將無法正常工作。
使用findstr
命令。 而不是find
更好,並且也需要使用正則表達式。
type somefile | findstr /v "^exit;"
要么
findstr /v "^exit;" somefile
祝好運
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.