Deleting the first two lines of a file using BASH or awk or sed or whatever

坚强是说给别人听的谎言 提交于 2019-12-02 17:58:15

Use tail:

tail -n+3 file

from the man page:

   -n, --lines=K
          output the last K lines, instead of the last 10; or use  -n  +K
          to output lines starting with the Kth

How about:

tail +3 file

OR

awk 'NR>2' file

OR

sed '1,2d' file

You're nearly there. Try this instead:

awk 'NR > 2 { print }' myfile

awk is rule based, and the rule appears bare (i.e., without braces) before the block it woud execute if it passes.

Also as Jaypal has pointed out, in awk if all you want to do is print the line that matches the rules you can even omit the action, thus simplifying the command to:

awk 'NR > 2' myfile

awk is based on pattern{action} statements. In your case, the pattern is NR>2 and the action you want to perform is print. This action is also the default action of awk.

So even though

awk 'NR>2{print}' filename

would work fine, you can shorten it to

awk 'NR>2' filename.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!