What is the easiest way to remove 1st and last line from file with awk?

前端 未结 3 386
遥遥无期
遥遥无期 2020-12-31 00:07

I am learning awk/gawk. So recently I just try to solve any problem with it to gain more practice opportunities.

My coworker asked a question yesterday,

3条回答
  •  孤独总比滥情好
    2020-12-31 00:21

    This does the trick:

    awk 'NR>2 {print last} {last=$0}'
    

    awk executes the action print last only when NR > 2 (that is, on all lines but the first 2). On all lines, it sets the variable last to the current line. So when awk reads the third line, it prints line 2 (which was stored in last). When it reads the last line (line n) it prints the content of line n-1. The net effect is that lines 2 through n-1 are printed.

提交回复
热议问题