【发布时间】:2022-01-24 12:34:11
【问题描述】:
我在文件中有很多重复的内容,如下所示。这些只是独特的内容。
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
但我发现这不是有效的做法。还有什么建议吗?
【问题讨论】:
-
如果
sed 's/="$/&Null"/' file.txt不是您所需要的,那么edit 您的问题可以更好地解释您的要求并提供更具代表性的示例。