最后添加分号但没有分号

Ank*_*Ank 2 unix awk grep sed

我有一个文件,其中大多数(不是所有)行以分号结尾.我想在那些没有以分号结尾的行的末尾添加分号.谢谢

Wil*_*ell 6

从技术上讲,这将有效:

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)


per*_*eal 6

使用 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)