在sed中使用双引号

时间:2016-04-15 08:14:38

标签: sed

我有一个sed命令来更改python文件中的内容:

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Result {

    @SerializedName("doc_no")
    @Expose
    private String docNo;
    @SerializedName("itembarcode")
    @Expose
    private String itembarcode;
    @SerializedName("net_wt")
    @Expose
    private String netWt;
    @SerializedName("gross_wt")
    @Expose
    private String grossWt;
    @SerializedName("stone_wt")
    @Expose
    private String stoneWt;
    @SerializedName("stone_amt")
    @Expose
    private String stoneAmt;
    @SerializedName("rate")
    @Expose
    private String rate;
    @SerializedName("making")
    @Expose
    private String making;
    @SerializedName("qty")
    @Expose
    private String qty;
    @SerializedName("net_rate")
    @Expose
    private String netRate;
    @SerializedName("item_total")
    @Expose
    private String itemTotal;
    @SerializedName("sum_total")
    @Expose
    private String sumTotal;

    /**
     *
     * @return
     * The docNo
     */
    public String getDocNo() {
        return docNo;
    }

    /**
     *
     * @param docNo
     * The doc_no
     */
    public void setDocNo(String docNo) {
        this.docNo = docNo;
    }

    /**
     *
     * @return
     * The itembarcode
     */
    public String getItembarcode() {
        return itembarcode;
    }

    /**
     *
     * @param itembarcode
     * The itembarcode
     */
    public void setItembarcode(String itembarcode) {
        this.itembarcode = itembarcode;
    }

    /**
     *
     * @return
     * The netWt
     */
    public String getNetWt() {
        return netWt;
    }

    /**
     *
     * @param netWt
     * The net_wt
     */
    public void setNetWt(String netWt) {
        this.netWt = netWt;
    }

    /**
     *
     * @return
     * The grossWt
     */
    public String getGrossWt() {
        return grossWt;
    }

    /**
     *
     * @param grossWt
     * The gross_wt
     */
    public void setGrossWt(String grossWt) {
        this.grossWt = grossWt;
    }

    /**
     *
     * @return
     * The stoneWt
     */
    public String getStoneWt() {
        return stoneWt;
    }

    /**
     *
     * @param stoneWt
     * The stone_wt
     */
    public void setStoneWt(String stoneWt) {
        this.stoneWt = stoneWt;
    }

    /**
     *
     * @return
     * The stoneAmt
     */
    public String getStoneAmt() {
        return stoneAmt;
    }

    /**
     *
     * @param stoneAmt
     * The stone_amt
     */
    public void setStoneAmt(String stoneAmt) {
        this.stoneAmt = stoneAmt;
    }

    /**
     *
     * @return
     * The rate
     */
    public String getRate() {
        return rate;
    }

    /**
     *
     * @param rate
     * The rate
     */
    public void setRate(String rate) {
        this.rate = rate;
    }

    /**
     *
     * @return
     * The making
     */
    public String getMaking() {
        return making;
    }

    /**
     *
     * @param making
     * The making
     */
    public void setMaking(String making) {
        this.making = making;
    }

    /**
     *
     * @return
     * The qty
     */
    public String getQty() {
        return qty;
    }

    /**
     *
     * @param qty
     * The qty
     */
    public void setQty(String qty) {
        this.qty = qty;
    }

    /**
     *
     * @return
     * The netRate
     */
    public String getNetRate() {
        return netRate;
    }

    /**
     *
     * @param netRate
     * The net_rate
     */
    public void setNetRate(String netRate) {
        this.netRate = netRate;
    }

    /**
     *
     * @return
     * The itemTotal
     */
    public String getItemTotal() {
        return itemTotal;
    }

    /**
     *
     * @param itemTotal
     * The item_total
     */
    public void setItemTotal(String itemTotal) {
        this.itemTotal = itemTotal;
    }

    /**
     *
     * @return
     * The sumTotal
     */
    public String getSumTotal() {
        return sumTotal;
    }

    /**
     *
     * @param sumTotal
     * The sum_total
     */
    public void setSumTotal(String sumTotal) {
        this.sumTotal = sumTotal;
    }

}

所以我想替换" ascii" by" utf-8"但这不起作用。 有人可以解释为什么它不起作用,我需要改变什么?

sed -i 's|encoding = "ascii"|encoding = "utf-8"|g' /usr/local/lib/python2.7/site.py

1 个答案:

答案 0 :(得分:0)

你是在安装Python之后做的,不是吗?您可以发布您的dockerfile以尝试重现您的问题吗?您是否尝试执行bash shell来测试该文件是否按预期更改了?

这是我工作的Dockerfile示例:

FROM ubuntu:14.04
MAINTAINER github/ojgarciab

# Use the "noninteractive" debconf frontend
ENV DEBIAN_FRONTEND noninteractive

# Add python
RUN apt-get update && apt-get install -y python && \
  apt-get clean

RUN sed -i 's|encoding = "ascii"|encoding = "utf-8"|g' /usr/lib/python2.7/site.py

ADD ejemplo.py /ejemplo.py
CMD [ "python", "/ejemplo.py" ]

在使用sed命令之前,输出为:

redstar@nvidiastar:~$ docker run -t python
ascii

现在的输出是:

redstar@nvidiastar:~$ docker run -t python
utf-8

ejemplo.py脚本的内容:

import sys
reload(sys)
print sys.getdefaultencoding();

请注意,使用的路径为/usr/lib/python2.7/site.py而非/usr/local/lib/python2.7/site.py