代码之家  ›  专栏  ›  技术社区  ›  user5359531

sed“$”匹配行首而不是行尾

  •  1
  • user5359531  · 技术社区  · 7 年前

    你可以用 sed 's|$|.tsv|' myfile.txt

    我要编辑的列如下所示:

    $ cut -f12 chickspress.tsv | sort -u | head
    Adipose_proteins
    Adrenal_gland
    Cerebellum
    Cerebrum
    Heart
    Hypothalamus
    Ovary
    Sciatic_nerve
    Testis
    Tissue
    

    sed 结果是错误的:

    $ cut -f12 chickspress.tsv | sort -u | sed -e 's|$|.tsv|'
    .tsvose_proteins
    .tsvnal_gland
    .tsvbellum
    .tsvbrum
    .tsvt
    .tsvthalamus
    .tsvy
    .tsvtic_nerve
    .tsvis
    .tsvue
    .tsvey
    .tsvr
    .tsv
    .tsvreas
    .tsvoral_muscle
    .tsventriculus
    

    .tsv

    我想可能有一些空白错误,所以我尝试了这个(macos):

    $ cut -f12 chickspress.tsv | sort -u | cat -ve
    Adipose_proteins^M$
    Adrenal_gland^M$
    Cerebellum^M$
    Cerebrum^M$
    Heart^M$
    Hypothalamus^M$
    Ovary^M$
    Sciatic_nerve^M$
    Testis^M$
    Tissue^M$
    kidney^M$
    liver^M$
    lung^M$
    pancreas^M$
    pectoral_muscle^M$
    proventriculus^M$
    

    ^M

    csv.DictWriter

    1 回复  |  直到 7 年前
        1
  •  3
  •   RavinderSingh13 Nikita Bakshi    7 年前

    awk '{sub(/\r$/,"")} 1' Input_file > temp_file && mv temp_file Input_file
    

    sed -i.bak '#s#\r$##' Input_file
    

    通过执行以下操作删除控件m字符,然后尝试您的命令。

    tr -d '\r' < Input_file > temp_file  && mv temp_file Input_file
    

    dos2unix 实用程序在您的系统中也可以使用它来删除这些字符。

    awk 以下内容:

    awk '{gsub(/\r/,"")} 1' Input_file > temp_file && mv temp_file Input_file
    

    sed

    sed -i.bak 's#\r##g' Input_file
    
    推荐文章