sed 如何删除所有行的前后空格

示例文件 /tmp/file

This is line one 
This is line two 
This is line three 
This is line four

删除所有前导空格

删除所有空格(不包括制表符)

# sed 's/^[ ]*//g' /tmp/file
 This is line one
 This is line two
 This is line three
         This is line four

删除所有前导空格(包括制表符)

# sed 's/^[t ]*//g' /tmp/file
 This is line one
 This is line two
 This is line three
 This is line four

删除所有尾随的空格

删除所有尾随空格(包括制表符)

# sed -i 's/[t ]*$//g' /tmp/file

删除前导和尾随空格

# sed  's/^[t ]*//g;s/[t ]*$//g' /tmp/file
This is line one
 This is line two
 This is line three
 This is line four

在这里,我将两个命令使用“;”组合在一起

日期:2020-06-02 22:17:33 来源:oir作者:oir