Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

head command to skip last few lines of file on MAC OSX

I want to output all lines of a file, but skip last 4, on Terminal.

As per UNIX man page following could be a solution.

head -n -4 main.m

MAN Page:

-n, --lines=[-]N print the first N lines instead of the first 10; with the lead- ing '-', print all but the last N lines of each file

I read man page here. http://unixhelp.ed.ac.uk/CGI/man-cgi?head

But on MAC OSx I get following error.

head: illegal line count -- -4

What else can be done to achieve this goal?

like image 644
Mohammad Avatar asked Apr 25 '13 04:04

Mohammad


People also ask

How do you use the head command on a Mac?

To quickly turn head pointer on or off using the Accessibility Shortcuts panel, press Option-Command-F5 (or if your Mac or Magic Keyboard has Touch ID, quickly press Touch ID three times).

Which command should I use to display the last 11 lines of a file?

Use the tail command to write the file specified by the File parameter to standard output beginning at a specified point. This displays the last 10 lines of the accounts file.

How do you skip a word in Mac terminal?

Hold option ( alt on some keyboards) and press the arrow keys left or right to move by word. Simple as that. Also ctrl e will take you to the end of the line and ctrl a will take you to the start.

How do you keep only the last n lines of a log file?

Use logrotate to do this automatically for you. there would be some exception case might come like no free space available to store log archive file (logrotate) in the server. For that kind of situation we have to keep only latest logs and remove other old log entries.


1 Answers

Use awk for example:

$ cat file
line 1
line 2
line 3
line 4
line 5
line 6
$ awk 'n>=4 { print a[n%4] } { a[n%4]=$0; n=n+1 }' file
line 1
line 2
$

It can be simplified to awk 'n>=4 { print a[n%4] } { a[n++%4]=$0 }' but I'm not sure if all awk implementations support it.

like image 107
pynexj Avatar answered Sep 22 '22 22:09

pynexj