Sugata Bagchi
Sugata Bagchi

Reputation: 143

Removing newline at end of file using bash shell script

I am trying to remove the last newline added to the file using bash script. I have got this - truncate -s $(($(stat -c '%s' foo.txt)-1)) foo.txt

here foo.txt the file name. but I want to parametrize the file name, I will pas the file name to the script and it should this remove the newline at last from that file. Request your help on this. I do not have linux in my machine and tried using cygwin but it is giving error while running the script.

Thanks

Upvotes: 2

Views: 1611

Answers (1)

anubhava
anubhava

Reputation: 784908

To remove last line if it is newline use this sedL

sed -i.bak '/^[[:blank:]]*$/{$d;}' foo.txt 

Upvotes: 1

Related Questions