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

前端 未结 8 1376
眼角桃花
眼角桃花 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:42

    EDITED ANSWER

    I created a script and put your text inside on my Desktop. this test file is saved as "old_file.txt"

    sometext
    moretext
    lastline
    

    Afterwards I wrote a small script to take the old file and eliminate the last character in the last line

    #!/bin/bash
    no_of_new_line_characters=`wc  '/root/Desktop/old_file.txt'|cut -d ' ' -f2`
    let "no_of_lines=no_of_new_line_characters+1"
    sed -n 1,"$no_of_new_line_characters"p  '/root/Desktop/old_file.txt' > '/root/Desktop/my_new_file'
    sed -n "$no_of_lines","$no_of_lines"p '/root/Desktop/old_file.txt'|sed 's/.$//g' >> '/root/Desktop/my_new_file'
    

    opening the new_file I created, showed the output as follows:

    sometext
    moretext
    lastlin
    

    I apologize for my previous answer (wasn't reading carefully)

提交回复
热议问题