Post History
Use head to keep all but the last N lines, overwriting the file: head -n -N input.txt > tmp && mv tmp input.txt Example: remove last 3 lines head -n -3 file.txt > tmp && ...
#4: Post edited
- Use `head` to keep all but the last *N* lines, overwriting the file:
- ```bash
- head -n -N input.txt > tmp && mv tmp input.txt
- ```
- Example: remove last 3 lines
- ```bash
- head -n -3 file.txt > tmp && mv tmp file.txt
- ```
If you want in-place editing without a temp file (GNU `sed` only):- ```bash
- sed -i "$(($(wc -l < file)-22)),\$d" file
```
- Use `head` to keep all but the last *N* lines, overwriting the file:
- ```bash
- head -n -N input.txt > tmp && mv tmp input.txt
- ```
- Example: remove last 3 lines
- ```bash
- head -n -3 file.txt > tmp && mv tmp file.txt
- ```
- If you want in-place editing without a temp file. The command
- ```bash
- sed -i "$(($(wc -l < file)-22)),\$d" file
- ```
- is used to delete the last 22 lines from a file named `file`.
#3: Post edited
- Use `head` to keep all but the last *N* lines, overwriting the file:
- ```bash
- head -n -N input.txt > tmp && mv tmp input.txt
- ```
- Example: remove last 3 lines
- ```bash
- head -n -3 file.txt > tmp && mv tmp file.txt
- ```
- If you want in-place editing without a temp file (GNU `sed` only):
- ```bash
- sed -i "$(($(wc -l < file)-22)),\$d" file
```But `head` + `mv` is safer and clearer.
- Use `head` to keep all but the last *N* lines, overwriting the file:
- ```bash
- head -n -N input.txt > tmp && mv tmp input.txt
- ```
- Example: remove last 3 lines
- ```bash
- head -n -3 file.txt > tmp && mv tmp file.txt
- ```
- If you want in-place editing without a temp file (GNU `sed` only):
- ```bash
- sed -i "$(($(wc -l < file)-22)),\$d" file
- ```
#2: Post edited
- Use `head` to keep all but the last *N* lines, overwriting the file:
- ```bash
- head -n -N input.txt > tmp && mv tmp input.txt
- ```
- Example: remove last 3 lines
- ```bash
- head -n -3 file.txt > tmp && mv tmp file.txt
- ```
- If you want in-place editing without a temp file (GNU `sed` only):
- ```bash
sed -i -e :a -e '$d;N;2,3ba' file.txt- ```
- But `head` + `mv` is safer and clearer.
- Use `head` to keep all but the last *N* lines, overwriting the file:
- ```bash
- head -n -N input.txt > tmp && mv tmp input.txt
- ```
- Example: remove last 3 lines
- ```bash
- head -n -3 file.txt > tmp && mv tmp file.txt
- ```
- If you want in-place editing without a temp file (GNU `sed` only):
- ```bash
- sed -i "$(($(wc -l < file)-22)),\$d" file
- ```
- But `head` + `mv` is safer and clearer.
#1: Initial revision
Use `head` to keep all but the last *N* lines, overwriting the file: ```bash head -n -N input.txt > tmp && mv tmp input.txt ``` Example: remove last 3 lines ```bash head -n -3 file.txt > tmp && mv tmp file.txt ``` If you want in-place editing without a temp file (GNU `sed` only): ```bash sed -i -e :a -e '$d;N;2,3ba' file.txt ``` But `head` + `mv` is safer and clearer.
