我在一個檔案中有很多重復的內容,如下所示。這些只是唯一的內容。
CHECKSUM="Y"
CHECKSUM="N"
CHECKSUM="U"
CHECKSUM="
我想用“Null”替換空欄位并需要輸出為:
CHECKSUM="Y"
CHECKSUM="N"
CHECKSUM="U"
CHECKSUM="Null"
我能想到的是:
#First find the matching content
cat file.txt | egrep 'CHECKSUM="Y"|CHECKSUM="N"|CHECKSUM="U"' > file_contain.txt
# Find the content where given string are not there
cat file.txt | egrep -v 'CHECKSUM="Y"|CHECKSUM="N"|CHECKSUM="U"' > file_donot_contain.txt
# Replace the string in content not found file
sed -i 's/CHECKSUM="/CHECKSUM="Null"/g' file_donot_contain.txt
# Merge the files
cat file_contain.txt file_donot_contain.txt > output.txt
但我發現這不是有效的做法。還有其他建議嗎?
uj5u.com熱心網友回復:
為了實作這一點,您需要使用$(并且也可以選擇^標記該行的開頭)來標記這是該行的結尾,而不僅僅是它的一部分:
sed -i s'/^CHECKSUM="$/CHECKSUM="Null"/' file.txt
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/391136.html
