仅从非空行中删除文件中的尾随空格

时间:2015-11-26 16:52:48

标签: sed spaces trailing

我知道如何从文件中删除所有尾随空格,例如:

public class UploadFeeEntryItem implements java.io.Serializable {

    private static final long serialVersionUID = -1209942461722257774L;

    private Long uploadItemId;
    //private Long uploadId;
    private Long recordId;
    private String errors;
    private String sender;
    private String customerId;
    private String accountNumber;
    private String routingNumber;

    public UploadFeeEntry getUploadFile() {
        return uploadFile;
    }
    public void setUploadFile(UploadFeeEntry uploadFile) {
        this.uploadFile = uploadFile;
    }
    public Long getUploadItemId() {
        return uploadItemId;
    }
    public void setUploadItemId(Long uploadItemId) {
        this.uploadItemId = uploadItemId;
    }

    public Long getRecordId() {
        return recordId;
    }
    public void setRecordId(Long recordId) {
        this.recordId = recordId;
    }
    public String getErrors() {
        return errors;
    }
    public void setErrors(String errors) {
        this.errors = errors;
    }
    public String getSender() {
        return sender;
    }
    public void setSender(String sender) {
        this.sender = sender;
    }
}

有没有办法,但不能只包含空格?

本着精神:

sed -i 's/  *$//' file

优选地,但不是必需的,具有sed。 任何Linux支持的解决方案都可以。

由于

1 个答案:

答案 0 :(得分:2)

请确保之前出现其他字符:

sed -r 's/([^\s])\s+$/\1/' file

检查是否出现非空格字符(\s),后跟任意数量的空格。如果是这样,只需打印此非空格字符,以便删除尾随空格。

测试

使用cat -vet查看标记:

$ cat -vet a
hello    $
       $
bye   $
$ sed -r 's/([^\s])\s+$/\1/' a | cat -vet -
hello$
 $
bye$