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 commands next.
  • fnr<=k-3 if fnr 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 of next command in previous block.

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -