![]() |
|
|
|
|
|||||||
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. Shell Script Page. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| delete first 100 lines from a file | salaathi | SUN Solaris | 3 | 11-15-2007 12:01 AM |
| delete first 100 lines rather than zero out of file | thepurple | SUN Solaris | 7 | 11-14-2007 09:35 AM |
| delete the lines from file | sameersam | Shell Programming and Scripting | 2 | 04-03-2006 10:32 PM |
| Delete lines in a file | umal | Shell Programming and Scripting | 6 | 02-08-2006 09:08 AM |
| delete all lines in file | strok | UNIX for Dummies Questions & Answers | 6 | 03-11-2002 06:27 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
delete n last lines of a file
Hello!!!
how can I delete the last n lines of a file??? Thanks |
| Forum Sponsor | ||
|
|
|
|||
|
Assuming you want to remove the last 22 lines:
Code:
tac filename | tail -n +23 | tac 1. Reverse the file. 2. Take all but the first 22 lines. 3. Reverse the file again (back to the original order). Note that the number tells it at which line to start, so 2 will cut off the first line, 50 will cut off 49 lines, etc. ShawnMilo |
|
|||
|
If you know the number of lines, subtract three and print up through that number.
Radoulov's solutions are superior, as usual, although they will read the entire file into memory at once, whereas the following basically operates the file a line at a time. Code:
lines=$(wc -l <inputfile) wanted=`expr $lines - 3` head -n $wanted inputfile >outputfile |
| Thread Tools | |
| Display Modes | |
|
|