Remove last 4 lines of a file

Hi,

I have a file from which I need to remove the last n lines.

I was successful in removing only the last line using :

sed '$d' file_name

Could you please help me with the command.

Thanks,
H2

take a look at the sed 1 liners.

Refer this post

sed "$((`wc -l f3|awk '{print $1}'`-3)),$ d" f3   #f3 filename

crazy one which i wouldn't suggest but works...

An awk version:

awk 'NR>c{print A[NR%c]} {A[NR%c]=$0}' c=4 file
1 Like

Or if c<=0 (modulus fails,of coarse if c=0, just use cat):

awk 'NR>c{print A[NR-c]?A[NR-c]:$0} {A[NR]=$0}' c=4 file

the unix command "head" is made for that type of operation. Use the following operation.

to remove the last two line of a file use:
 
head -n-2 yourfile
1 Like

@ scottazz : classic...:b:

Only works on some systems.