bash - How can I automatically disregard the last three lines of a file? -
i using following command:
head -36 file.txt > file1.txt
where file 39 lines , thing i'm checking file 39 lines , place number 36 in comment. there way command calculates number of lines , deduct last 3 lines ?
and here awk solution (no process substitution, piping, etc.)
awk 'nr==fnr{k++;next}fnr<=k-3' file.txt file.txt
explanation:
- we add file.txt 2 times, awk reads twice. first time counts total number of lines in file, , second time prints lines, except last three
nr==fnr{k++;next}
:nr
variable record number (including both filee), ,fnr
record number in current file. equal records in first file. so, first file count lines in it,k++
, , skip remaining commandsnext
.fnr<=k-3
iffnr
variable smaller total lines in file (which counted in previous bullet) - 3, expression evaluates true, , line printed. otherwise, expression evaluates false (for last 3 lines), , line not printed. happens second file, because ofnext
command in previous block.
Comments
Post a Comment