How can I delete the last line of a file without reading the entire file or rewriting it in any temp file. I dont want to use sed as it reads the entire file into memory. There is a blank line at last. I want to remove that and save the change to that file only so that the file does not contain blank line at last.
Asked
Active
Viewed 2,364 times
3 Answers
1
simple sed command to delete last line:
sed '$d' <file>
here in sed $ is the last line.
You can try awk command:
awk 'NR > 1{print t} {t = $0}END{if (NF) print }' file
Using cat:
cat file.txt | head -n -1 > new_file.txt
danglingpointer
- 4,388
- 3
- 22
- 40
0
The easiest way I can think of is:
sed -i '${/^$/d}' filename
edited to delete only blank end line.
Your only option not using sed that won't read the entire file is to stat the file to get the size and then use dd to skip to the end before you start reading. However, telling sed to only operate on the last line, does essentially that.
David C. Rankin
- 75,900
- 6
- 54
- 79
-
sed -i -u ... -u mean *unbuffered*, so lower memory use – NeronLeVelu Apr 06 '17 at 08:59
-
In this case since you are jumping to the end, you would certainly want to use a buffered read to speed the process, especially for large files. – David C. Rankin Apr 07 '17 at 05:37
-
buffered speed the process of writing but the problem is memory here, not process speed says OP – NeronLeVelu Apr 07 '17 at 08:27
-
1I get `sed: 1: "${/^$/d}": extra characters at the end of d command`. – Iulian Onofrei Mar 13 '18 at 14:29
-
2It is necessary to put a semicolon at the end of 'd', otherwise you will get @IulianOnofrei error – Aug 23 '18 at 09:31
-
For me, only [`sed -i '.txt' '${/^$/d;}' test.txt`](https://www.mkyong.com/mac/sed-command-hits-undefined-label-error-on-mac-os-x/) worked, because after adding the semicolon it failed with `sed: 1: "test.txt": undefined label 'est.txt'`. – Iulian Onofrei Aug 23 '18 at 09:36
-
[`sed -i '' '${/^$/d;}' test.txt`](https://mpdaugherty.wordpress.com/2010/05/27/difference-with-sed-in-place-editing-on-mac-os-x-vs-linux/) also works. – Iulian Onofrei Aug 23 '18 at 09:39
-
Yes, that would work too. Good comment. – David C. Rankin Aug 23 '18 at 09:47
0
Take a look at
Remove the last line from a file in Bash
Edit: I tested
dd if=/dev/null of=<filename> bs=1 seek=$(echo $(stat --format=%s <filename> ) - $( tail -n1 <filename> | wc -c) | bc )
and it does what you want