Unix 在没有分号的末尾添加分号
我有一个文件,其中大多数(不是全部)行以分号结尾。我只想在那些没有以分号结尾的行的末尾加上分号。感谢从技术上讲,这将起作用:Unix 在没有分号的末尾添加分号,unix,sed,awk,grep,Unix,Sed,Awk,Grep,我有一个文件,其中大多数(不是全部)行以分号结尾。我只想在那些没有以分号结尾的行的末尾加上分号。感谢从技术上讲,这将起作用: sed '/;$/!s/$/;/' input 但您可能关心尾随空格,因此: sed '/; *$/!s/$/;/' input 如果您的sed支持\s: sed '/;\s*$/!s/$/;/' input 或者您可以使用: sed '/;[[:space:]]*$/!s/$/;/' input 使用sed: sed -i '/[^;] *$/s/$/;/'
sed '/;$/!s/$/;/' input
但您可能关心尾随空格,因此:
sed '/; *$/!s/$/;/' input
如果您的sed支持\s
:
sed '/;\s*$/!s/$/;/' input
或者您可以使用:
sed '/;[[:space:]]*$/!s/$/;/' input
使用sed:
sed -i '/[^;] *$/s/$/;/' input_file
这意味着:
-i overwrite the original file with new contents
/[^;] *$/ find lines that do not contain a `;` at the end (after
ignoring trailing spaces)
s/$/;/ add a semicolon at the end
除非
-i
不在位编辑文件,而是用新文件替换文件-i
是最好避免的非标准选项。我喜欢这个答案,因为它解释了正则表达式。谢谢!