How can I remove the last character of a file in unix?

前端 未结 8 1328
眼角桃花
眼角桃花 2020-11-30 05:35

Say I have some arbitrary multi-line text file:

sometext
moretext
lastline

How can I remove only the last character (the e, not the newline

相关标签:
8条回答
  • 2020-11-30 05:58

    sed 's/.$//' filename | tee newFilename

    This should do your job.

    0 讨论(0)
  • 2020-11-30 06:07

    If the goal is to remove the last character in the last line, this awk should do:

    awk '{a[NR]=$0} END {for (i=1;i<NR;i++) print a[i];sub(/.$/,"",a[NR]);print a[NR]}' file
    sometext
    moretext
    lastlin
    

    It store all data into an array, then print it out and change last line.

    0 讨论(0)
提交回复
热议问题