从技术上讲,这将有效:
sed '/;$/!s/$/;/' input
Run Code Online (Sandbox Code Playgroud)
但是你可能关心尾随空格,所以:
sed '/; *$/!s/$/;/' input
Run Code Online (Sandbox Code Playgroud)
如果你的sed支持\s:
sed '/;\s*$/!s/$/;/' input
Run Code Online (Sandbox Code Playgroud)
或者您可以使用:
sed '/;[[:space:]]*$/!s/$/;/' input
Run Code Online (Sandbox Code Playgroud)
使用 sed:
sed -i '/[^;] *$/s/$/;/' input_file
Run Code Online (Sandbox Code Playgroud)
意思是:
-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
Run Code Online (Sandbox Code Playgroud)