Command to remove the first N number of lines in input

awkcommand lineheadsedtail

Background

I am running an SSH server and have this user that I want to delete. I cannot delete this user because he is currently running a few processes that I need to kill first.

This is the pipeline I am using currently using to find out all the process ids of the user I am currently using:

ps -u user | awk '{print $1;}'

The output looks like this:

PID
2121
2122
2124
2125
2369
2370

I want to pipe this to kill -9 to kill all processes so I can delete this stupid user like this:

ps -u user | awk '{print $1;}' | sudo xargs kill -9

But this does not work because of the PID header:

kill: failed to parse argument: 'PID'

The question

I am thinking that there has to be a simple Unix command to remove the first line of input.

I am aware that I can use tail for this but I don't want to count how many lines the input contains to figure out exactly how many I want to display.

I am looking for something like head or tail but inverted (instead of displaying only the first/last part of the stream it displays everything but the start/end of stream).

Note

I managed to solve this issue I had by simply adding | grep [[:digit:]] after my awk command but I am still looking for a way to delete first line of a file as I think would be quite useful in other scenarios.

Best Answer

NOTE: if your system already has pgrep/pkill then you are re-inventing the wheel here. If your system doesn't have these utilities, then you should be able to format the output of ps to get the unencumbered PID list directly e.g. ps -u user -opid=

If you are already using awk, there is no need to pipe through an additional process in order to remove the first line (record): simply add a condition on the record number NR

ps -u user | awk 'NR>1{print $1;}'

Since you mention head and tail, the formula you probably want in this case is tail -n +2

Related Question